Added extra info on parsing more complex json.

Matthew Ogborne
2016-03-01 10:39:41 +00:00
parent d3dd10fa0f
commit 60526b419a

@@ -145,3 +145,33 @@ You can also iterate through the key-value pairs of the object:
Serial.println(it->key);
Serial.println(it->value.asString());
}
## Advanced Example
Let's take what we've learned above up a gear :)
Our JSON array is now:
char json[] = "{\"data\":{\"time\":{\"day\":1,\"month\":3,\"year\":16,\"hours\":9,\"mins\":59,\"secs\":14}}}";
We parse it and check it as before with:
JsonObject& root = jsonBuffer.parseObject(json);
// Test if parsing succeeds.
if (!root.success()) {
Serial.println("parseObject() failed");
return;
}
Now to get the data, we can follow the object downwards, just like you would do with PHP etc...:
int day = root["data"]["time"]["day"];
int month = root["data"]["time"]["month"];
int year = root["data"]["time"]["year"];
int hours = root["data"]["time"]["hours"];
int mins = root["data"]["time"]["mins"];
int secs = root["data"]["time"]["secs"];
This allows for more complex data to be processed, the time as in this example or numerous values from a single function. Just make sure your jsonBuffer is large enough to handle the larger objects!
See the file called "JsonParserExample_Advanced.ino" in "ArduinoJson/examples/JsonParserExample/"