mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-08-18 03:51:21 +02:00
Updated Quick Start (markdown)
@@ -2,33 +2,37 @@
|
|||||||
|
|
||||||
#### Decoding / Parsing
|
#### Decoding / Parsing
|
||||||
|
|
||||||
char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
|
```c++
|
||||||
|
char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
|
||||||
|
|
||||||
StaticJsonBuffer<200> jsonBuffer;
|
StaticJsonBuffer<200> jsonBuffer;
|
||||||
|
|
||||||
JsonObject& root = jsonBuffer.parseObject(json);
|
JsonObject& root = jsonBuffer.parseObject(json);
|
||||||
|
|
||||||
const char* sensor = root["sensor"];
|
const char* sensor = root["sensor"];
|
||||||
long time = root["time"];
|
long time = root["time"];
|
||||||
double latitude = root["data"][0];
|
double latitude = root["data"][0];
|
||||||
double longitude = root["data"][1];
|
double longitude = root["data"][1];
|
||||||
|
```
|
||||||
|
|
||||||
[See complete guide](../Decoding-JSON)
|
[See complete guide](../Decoding-JSON)
|
||||||
|
|
||||||
#### Encoding / Generating
|
#### Encoding / Generating
|
||||||
|
|
||||||
StaticJsonBuffer<200> jsonBuffer;
|
```c++
|
||||||
|
StaticJsonBuffer<200> jsonBuffer;
|
||||||
|
|
||||||
JsonObject& root = jsonBuffer.createObject();
|
JsonObject& root = jsonBuffer.createObject();
|
||||||
root["sensor"] = "gps";
|
root["sensor"] = "gps";
|
||||||
root["time"] = 1351824120;
|
root["time"] = 1351824120;
|
||||||
|
|
||||||
JsonArray& data = root.createNestedArray("data");
|
JsonArray& data = root.createNestedArray("data");
|
||||||
data.add(48.756080, 6); // 6 is the number of decimals to print
|
data.add(48.756080, 6); // 6 is the number of decimals to print
|
||||||
data.add(2.302038, 6); // if not specified, 2 digits are printed
|
data.add(2.302038, 6); // if not specified, 2 digits are printed
|
||||||
|
|
||||||
root.printTo(Serial);
|
root.printTo(Serial);
|
||||||
// This prints:
|
// This prints:
|
||||||
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
|
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
|
||||||
|
```
|
||||||
|
|
||||||
[See complete guide](../Encoding-JSON)
|
[See complete guide](../Encoding-JSON)
|
Reference in New Issue
Block a user