2021-03-29 17:14:01 +02:00
|
|
|
// ArduinoJson - https://arduinojson.org
|
2022-01-01 10:00:54 +01:00
|
|
|
// Copyright © 2014-2022, Benoit BLANCHON
|
2016-11-06 17:48:32 +01:00
|
|
|
// MIT License
|
2017-12-11 15:19:28 +01:00
|
|
|
//
|
|
|
|
// This example shows the different ways you can use String with ArduinoJson.
|
|
|
|
//
|
|
|
|
// Use String objects sparingly, because ArduinoJson duplicates them in the
|
2019-01-29 14:09:09 +01:00
|
|
|
// JsonDocument. Prefer plain old char[], as they are more efficient in term of
|
2017-12-11 15:19:28 +01:00
|
|
|
// code size, speed, and memory usage.
|
2019-03-04 12:17:41 +01:00
|
|
|
//
|
|
|
|
// https://arduinojson.org/v6/example/string/
|
2016-11-06 17:48:32 +01:00
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
|
|
|
void setup() {
|
2019-01-14 10:32:19 +01:00
|
|
|
DynamicJsonDocument doc(1024);
|
2016-11-06 17:48:32 +01:00
|
|
|
|
|
|
|
// You can use a String as your JSON input.
|
2019-01-29 14:09:09 +01:00
|
|
|
// WARNING: the string in the input will be duplicated in the JsonDocument.
|
2016-11-06 17:48:32 +01:00
|
|
|
String input =
|
|
|
|
"{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
|
2018-04-17 21:27:45 +02:00
|
|
|
deserializeJson(doc, input);
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.as<JsonObject>();
|
2016-11-06 17:48:32 +01:00
|
|
|
|
|
|
|
// You can use a String to get an element of a JsonObject
|
|
|
|
// No duplication is done.
|
2018-04-17 21:27:45 +02:00
|
|
|
long time = obj[String("time")];
|
2016-11-06 17:48:32 +01:00
|
|
|
|
|
|
|
// You can use a String to set an element of a JsonObject
|
2019-01-29 14:09:09 +01:00
|
|
|
// WARNING: the content of the String will be duplicated in the JsonDocument.
|
2018-04-17 21:27:45 +02:00
|
|
|
obj[String("time")] = time;
|
2016-11-06 17:48:32 +01:00
|
|
|
|
|
|
|
// You can get a String from a JsonObject or JsonArray:
|
2019-01-29 14:09:09 +01:00
|
|
|
// No duplication is done, at least not in the JsonDocument.
|
2018-04-17 21:27:45 +02:00
|
|
|
String sensor = obj["sensor"];
|
2016-11-24 18:11:02 +01:00
|
|
|
|
|
|
|
// Unfortunately, the following doesn't work (issue #118):
|
2018-04-17 21:27:45 +02:00
|
|
|
// sensor = obj["sensor"]; // <- error "ambiguous overload for 'operator='"
|
2016-11-24 18:11:02 +01:00
|
|
|
// As a workaround, you need to replace by:
|
2018-04-17 21:27:45 +02:00
|
|
|
sensor = obj["sensor"].as<String>();
|
2016-11-06 17:48:32 +01:00
|
|
|
|
|
|
|
// You can set a String to a JsonObject or JsonArray:
|
2019-01-29 14:09:09 +01:00
|
|
|
// WARNING: the content of the String will be duplicated in the JsonDocument.
|
2018-04-17 21:27:45 +02:00
|
|
|
obj["sensor"] = sensor;
|
2016-11-06 17:48:32 +01:00
|
|
|
|
2018-07-12 09:08:20 +02:00
|
|
|
// It works with serialized() too:
|
|
|
|
obj["sensor"] = serialized(sensor);
|
2018-01-18 09:43:37 +01:00
|
|
|
|
2016-11-06 17:48:32 +01:00
|
|
|
// You can also concatenate strings
|
2019-01-29 14:09:09 +01:00
|
|
|
// WARNING: the content of the String will be duplicated in the JsonDocument.
|
2018-04-17 21:27:45 +02:00
|
|
|
obj[String("sen") + "sor"] = String("gp") + "s";
|
2016-11-06 17:48:32 +01:00
|
|
|
|
2016-12-23 14:45:32 +01:00
|
|
|
// You can compare the content of a JsonObject with a String
|
2018-04-17 21:27:45 +02:00
|
|
|
if (obj["sensor"] == sensor) {
|
2016-12-23 14:45:32 +01:00
|
|
|
// ...
|
|
|
|
}
|
|
|
|
|
2016-11-06 17:48:32 +01:00
|
|
|
// Lastly, you can print the resulting JSON to a String
|
|
|
|
String output;
|
2018-04-17 21:27:45 +02:00
|
|
|
serializeJson(doc, output);
|
2016-11-06 17:48:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
// not used in this example
|
|
|
|
}
|
2017-12-11 15:19:28 +01:00
|
|
|
|
2019-03-04 12:17:41 +01:00
|
|
|
// See also
|
|
|
|
// --------
|
|
|
|
//
|
|
|
|
// https://arduinojson.org/ contains the documentation for all the functions
|
|
|
|
// used above. It also includes an FAQ that will help you solve any problem.
|
|
|
|
//
|
|
|
|
// The book "Mastering ArduinoJson" contains a quick C++ course that explains
|
|
|
|
// how your microcontroller stores strings in memory. On several occasions, it
|
|
|
|
// shows how you can avoid String in your program.
|
|
|
|
// Learn more at https://arduinojson.org/book/
|
|
|
|
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
|