Square Brackets

I'm trying to understand the AduinoJson library.
In the API documentation there are the following lines:

char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";

StaticJsonBuffer<200> jsonBuffer;

JsonObject& root = jsonBuffer.parseObject(json);

const char* sensor = root["sensor"];
long time          = root["time"];
double latitude    = root["data"][0];
double longitude   = root["data"][1];

I've seen square brackets being used as a marker for an array and containing the index for an array, but I can't understand how they are being used there. I can understand that object root is parsed version of the jsonBuffer, and that the phrase "sensor" is doing a string search, but this clause:
root["sensor"] I don't understand at all.
If anyone can tell explain it I would be grateful. :slight_smile:

rajdarge:
I'm trying to understand the AduinoJson library.

URL?

here is the link for the library

and the wiki with examples:

thanks for the responses :slight_smile:

Delta_G:
Maybe that class overloads the brackets operator to do something else.

Yup. This looks like the operator of interest...
https://github.com/bblanchon/ArduinoJson/blob/master/include/ArduinoJson/JsonObject.hpp#L59

...but I can't understand how they are being used there.

C++ allows operators, like array reference ([]), to be overridden. It is really just "syntax sugar". The same thing can be accomplished using a method. Given your confusion, overriding the array reference operator was a bad choice by the library's author. The point of overriding an operator is to make the source code more readable not less.

The author could have just as easily done something like this...

const char* sensor = root.getValueAsStringOfNodeNamed( "sensor" );
long time          = root.getValueAsLongOfNodeNamed( "time" );

Ok that makes sense now,
as you say it was overloading the [] operator with a class function
so when implementing the square brackets it was using the function rather than the default array operator.
It is doing this with data type const char*, String& , a JsonObjectKey.
I can't see it being able to overload with a float, int, word etc.

I have a section of someone elses code that I am trying to bend to my will and it uses this line:

const float temp_c = current["temp_c"];

It compiles ok but every time that the program gets to that line, the chip crashes and gives me a memory dump.

I am using it to send data to a WUNDERGROUND account, obtain TZ and DST info to parse the time offset for local time, along with temp and weather. I am using an ESP8266, but sending stuff to a Pro-mini that uses it to log temp data with UTC time and offset to a SD card. Just in case you were wondering. It might be a problem with the local version of the ArduinoJson Library rather than the library itself. The Esp8266 has a lot of quirks, I have found.

I couldn't imagine temperature being constant. Why did you tell the compiler that you wanted to make it a constant?

to be honest I couldn't understand that either, but even converting it to a float variable, it still caused an error. Not in compilation, in execution.

this is a link to that code: