mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 08:42:39 +01:00 
			
		
		
		
	Added StaticJsonDocument and DynamicJsonDocument.
Removed StaticJsonArray and DynamicJsonArray. Removed StaticJsonObject and DynamicJsonObject. Removed StaticJsonVariant and DynamicJsonVariant.
This commit is contained in:
		| @@ -11,50 +11,51 @@ | ||||
| #include <ArduinoJson.h> | ||||
|  | ||||
| void setup() { | ||||
|   DynamicJsonObject root; | ||||
|   DynamicJsonDocument doc; | ||||
|  | ||||
|   // You can use a String as your JSON input. | ||||
|   // WARNING: the content of the String will be duplicated in the JsonBuffer. | ||||
|   String input = | ||||
|       "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; | ||||
|   deserializeJson(root, input); | ||||
|   deserializeJson(doc, input); | ||||
|   JsonObject& obj = doc.as<JsonObject>(); | ||||
|  | ||||
|   // You can use a String to get an element of a JsonObject | ||||
|   // No duplication is done. | ||||
|   long time = root[String("time")]; | ||||
|   long time = obj[String("time")]; | ||||
|  | ||||
|   // You can use a String to set an element of a JsonObject | ||||
|   // WARNING: the content of the String will be duplicated in the JsonBuffer. | ||||
|   root[String("time")] = time; | ||||
|   obj[String("time")] = time; | ||||
|  | ||||
|   // You can get a String from a JsonObject or JsonArray: | ||||
|   // No duplication is done, at least not in the JsonBuffer. | ||||
|   String sensor = root["sensor"]; | ||||
|   String sensor = obj["sensor"]; | ||||
|  | ||||
|   // Unfortunately, the following doesn't work (issue #118): | ||||
|   // sensor = root["sensor"]; // <-  error "ambiguous overload for 'operator='" | ||||
|   // sensor = obj["sensor"]; // <-  error "ambiguous overload for 'operator='" | ||||
|   // As a workaround, you need to replace by: | ||||
|   sensor = root["sensor"].as<String>(); | ||||
|   sensor = obj["sensor"].as<String>(); | ||||
|  | ||||
|   // You can set a String to a JsonObject or JsonArray: | ||||
|   // WARNING: the content of the String will be duplicated in the JsonBuffer. | ||||
|   root["sensor"] = sensor; | ||||
|   obj["sensor"] = sensor; | ||||
|  | ||||
|   // It works with RawJson too: | ||||
|   root["sensor"] = RawJson(sensor); | ||||
|   obj["sensor"] = RawJson(sensor); | ||||
|  | ||||
|   // You can also concatenate strings | ||||
|   // WARNING: the content of the String will be duplicated in the JsonBuffer. | ||||
|   root[String("sen") + "sor"] = String("gp") + "s"; | ||||
|   obj[String("sen") + "sor"] = String("gp") + "s"; | ||||
|  | ||||
|   // You can compare the content of a JsonObject with a String | ||||
|   if (root["sensor"] == sensor) { | ||||
|   if (obj["sensor"] == sensor) { | ||||
|     // ... | ||||
|   } | ||||
|  | ||||
|   // Lastly, you can print the resulting JSON to a String | ||||
|   String output; | ||||
|   serializeJson(root, output); | ||||
|   serializeJson(doc, output); | ||||
| } | ||||
|  | ||||
| void loop() { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user