mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-08-16 19:11:15 +02:00
Added JsonArray iterator
57
Examples.md
57
Examples.md
@@ -1,3 +1,29 @@
|
|||||||
|
## Example 1: complex nested object
|
||||||
|
|
||||||
|
From [issue #85](https://github.com/bblanchon/ArduinoJson/issues/85):
|
||||||
|
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"error": {
|
||||||
|
"address": "",
|
||||||
|
"description": "link button not pressed",
|
||||||
|
"type": "101"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
Can be generated by the following code:
|
||||||
|
|
||||||
|
DynamicJsonBuffer jsonBuffer;
|
||||||
|
JsonArray& root = jsonBuffer.createArray();
|
||||||
|
JsonObject& error = root.createNestedObject().createNestedObject("error");
|
||||||
|
error["address"] = "";
|
||||||
|
error["description"] = "link button not pressed";
|
||||||
|
error["type"] = "101";
|
||||||
|
root.prettyPrintTo(Serial);
|
||||||
|
|
||||||
|
Don't forget to replace the `DynamicJsonBuffer` by a `StaticJsonBuffer` if you need to run on an embedded platform like an Arduino.
|
||||||
|
|
||||||
## Example 2: get the weather data from aviationweather.gov or geonames.org
|
## Example 2: get the weather data from aviationweather.gov or geonames.org
|
||||||
char *MTserverName = "api.geonames.org"; // server name
|
char *MTserverName = "api.geonames.org"; // server name
|
||||||
String cmd = "GET /weatherIcaoJSON?ICAO="; // prepare the command to send to the web server after
|
String cmd = "GET /weatherIcaoJSON?ICAO="; // prepare the command to send to the web server after
|
||||||
@@ -28,29 +54,16 @@ getting the data on JSON format and parsing the buffer:
|
|||||||
MET->MTwindspeed = _ooo ["windSpeed"]; // speed
|
MET->MTwindspeed = _ooo ["windSpeed"]; // speed
|
||||||
MET->MTobservationtime= _ooo ["datetime"]; // time
|
MET->MTobservationtime= _ooo ["datetime"]; // time
|
||||||
|
|
||||||
|
## Example 3: use iterators
|
||||||
|
|
||||||
## Example 1: complex nested object
|
|
||||||
|
|
||||||
From [issue #85](https://github.com/bblanchon/ArduinoJson/issues/85):
|
```c++
|
||||||
|
JsonArray & array;
|
||||||
|
|
||||||
[
|
for(JsonArray::iterator it=array.begin(); it!=array.end(); ++it) {
|
||||||
{
|
const char* value = *it;
|
||||||
"error": {
|
Serial.println(value);
|
||||||
"address": "",
|
// or
|
||||||
"description": "link button not pressed",
|
Serial.println(it->as<const char*>());
|
||||||
"type": "101"
|
|
||||||
}
|
}
|
||||||
}
|
```
|
||||||
]
|
|
||||||
|
|
||||||
Can be generated by the following code:
|
|
||||||
|
|
||||||
DynamicJsonBuffer jsonBuffer;
|
|
||||||
JsonArray& root = jsonBuffer.createArray();
|
|
||||||
JsonObject& error = root.createNestedObject().createNestedObject("error");
|
|
||||||
error["address"] = "";
|
|
||||||
error["description"] = "link button not pressed";
|
|
||||||
error["type"] = "101";
|
|
||||||
root.prettyPrintTo(Serial);
|
|
||||||
|
|
||||||
Don't forget to replace the `DynamicJsonBuffer` by a `StaticJsonBuffer` if you need to run on an embedded platform like an Arduino.
|
|
Reference in New Issue
Block a user