Access to an array element by its key

Hi,

I'm qui te new on Arduino and it's like i'm getting trouble with some cases :
I'm using JsonParser Library. All work well, so I've started to ckean up my code and refactor my code.

  1. I've tried to create a function that return JsonObject but it doesn't accept it :
JsonObject getJsonObject() {
}

Just this chunk of code generate this error : JsonObject does not name a type. So bad, when I delete this function, my other code (which uses JsonObject) works... what the point ?

  1. to workaround this problem, I've created another function but I still have difficulties.
    When getting JsonObject result, we can access its values by writing this :
int myresult = myjson["key1"][0]

I've tried to create a function that get the key name in parameter but I can't manage to make it work :frowning:

I tried this

function GetParsedValue(char* Key) {
jsonObject result = code to get my json;
return result[Key];
}

but it doesn't work. I've tried to get my answer on the internet but could not find anything.
If you have a clue...
Thanks
Steeve

Please post a complete piece of code that demonstrates this problem, inside code tags.

Also post a link to the JSON decoder you are using. It is impossible to comment without seeing the code.

int myresult = myjson["key1"][0]

I doubt that will work as expected in C++. Where did this idea come from?

Hello Nick,

I’m using this parser : https://github.com/bblanchon/ArduinoJson/tree/master/JsonParser
You will see that you can get a JsonObject from this library.
My main idea is this : Create a function that will get http response, parse it into a JsonObject and then return this object.

I’ve tried this :

#include <JsonParser.h>

using namespace ArduinoJson::Parser;

void Test() {
  JsonObject jsonResponse = GetParsedResponse("myurl");
  DoSomethingWith(jsonResponse["data"][0]);
}

JsonObject GetParsedResponse(String url) {
    //HTTPClient gets a JSON Http Response through url argument
   char json [] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
    JsonParser<16> parser;
    JsonObject root = parser.parse(json);
    if (!root.success())
      return null;
    return root;
}

When verifying compilation, it says : JsonObject does not name a type.
Sorry for my lack in C++, I come from .Net world and things are simplier about this kind of things ^^
Thanks for your help

I can’t get it to compile because you left some of it out, but this gets closer:

#include <JsonParser.h>

using namespace ArduinoJson::Parser;

JsonObject GetParsedResponse(String url);  // <--- function prototype

void Test() {
  JsonObject jsonResponse = GetParsedResponse("myurl");
  DoSomethingWith(jsonResponse["data"][0]);
}

JsonObject GetParsedResponse(String url) {
    //HTTPClient gets a JSON Http Response through url argument
   char json [] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
    JsonParser<16> parser;
    JsonObject root = parser.parse(json);
    if (!root.success())
      return null;
    return root;
}

I added the line marked “function prototype”. Normally in C++ you need to declare functions before you use them, but the Arduino IDE tries to do that for you, not always successfully.

Great ! that’s kinda tricky but it works :slight_smile:
I found that to be able to set a variable to null, I have to set its ptr to NULL like this :

#include <JsonParser.h>

using namespace ArduinoJson::Parser;

JsonObject GetParsedResponse(String url);

void Test() {
  GetParsedResponse("");
}

JsonObject GetParsedResponse(String url) {
    //HTTPClient gets a JSON Http Response through url argument
   char json [] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
    JsonParser<16> parser;
    JsonObject root = parser.parse(json);
    if (!root.success()) {
      JsonObject *p = &root;
       p = NULL;
      return root;
    }
    return root;
}

Thanks for all Nick :slight_smile: