|
|
@@ -8,23 +8,26 @@
|
|
|
#include "ArduinoJson.h"
|
|
|
|
|
|
int main() {
|
|
|
- // Root JSON object
|
|
|
+ // Allocate the JSON document
|
|
|
//
|
|
|
- // Inside the brackets, 300 is the size of the memory pool in bytes.
|
|
|
+ // Inside the brackets, 200 is the capacity of the memory pool in bytes.
|
|
|
// Don't forget to change this value to match your JSON document.
|
|
|
- // Use arduinojson.org/assistant to compute the capacity.
|
|
|
+ // Use arduinojson.org/v6/assistant to compute the capacity.
|
|
|
StaticJsonDocument<300> doc;
|
|
|
|
|
|
// StaticJsonDocument<N> allocates memory on the stack, it can be
|
|
|
- // replaced by DynamicJsonObject which allocates in the heap.
|
|
|
+ // replaced by DynamicJsonDocument which allocates in the heap.
|
|
|
//
|
|
|
- // DynamicJsonObject doc(200);
|
|
|
+ // DynamicJsonDocument doc(200);
|
|
|
|
|
|
// JSON input string.
|
|
|
//
|
|
|
- // It's better to use a char[] as shown here.
|
|
|
- // If you use a const char* or a String, ArduinoJson will
|
|
|
- // have to make a copy of the input in the JsonBuffer.
|
|
|
+ // Using a char[], as shown here, enables the "zero-copy" mode. This mode uses
|
|
|
+ // the minimal amount of memory because the JsonDocument stores pointers to
|
|
|
+ // the input buffer.
|
|
|
+ // If you use another type of input, ArduinoJson must copy the strings from
|
|
|
+ // the input to the JsonDocument, so you need to increase the capacity of the
|
|
|
+ // JsonDocument.
|
|
|
char json[] =
|
|
|
"{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
|
|
|
|
|
|
@@ -37,17 +40,14 @@ int main() {
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
- // Get the root object in the document
|
|
|
- JsonObject root = doc.as<JsonObject>();
|
|
|
-
|
|
|
// Fetch values.
|
|
|
//
|
|
|
// Most of the time, you can rely on the implicit casts.
|
|
|
// In other case, you can do doc["time"].as<long>();
|
|
|
- const char* sensor = root["sensor"];
|
|
|
- long time = root["time"];
|
|
|
- double latitude = root["data"][0];
|
|
|
- double longitude = root["data"][1];
|
|
|
+ const char* sensor = doc["sensor"];
|
|
|
+ long time = doc["time"];
|
|
|
+ double latitude = doc["data"][0];
|
|
|
+ double longitude = doc["data"][1];
|
|
|
|
|
|
// Print values.
|
|
|
std::cout << sensor << std::endl;
|