mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Added comments in examples
This commit is contained in:
		| @@ -13,12 +13,36 @@ void setup() { | |||||||
|     // wait serial port initialization |     // wait serial port initialization | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   // Memory pool for JSON object tree. | ||||||
|  |   // | ||||||
|  |   // Inside the brackets, 200 is the size of the pool in bytes. | ||||||
|  |   // If the JSON object is more complex, you need to increase that value. | ||||||
|   StaticJsonBuffer<200> jsonBuffer; |   StaticJsonBuffer<200> jsonBuffer; | ||||||
|  |  | ||||||
|  |   // StaticJsonBuffer allocates memory on the stack, it can be | ||||||
|  |   // replaced by DynamicJsonBuffer which allocates in the heap. | ||||||
|  |   // It's simpler but less efficient. | ||||||
|  |   // | ||||||
|  |   // DynamicJsonBuffer  jsonBuffer; | ||||||
|  |  | ||||||
|  |   // Create the root of the object tree. | ||||||
|  |   // | ||||||
|  |   // It's a reference to the JsonObject, the actual bytes are inside the | ||||||
|  |   // JsonBuffer with all the other nodes of the object tree. | ||||||
|  |   // Memory is freed when jsonBuffer goes out of scope. | ||||||
|   JsonObject& root = jsonBuffer.createObject(); |   JsonObject& root = jsonBuffer.createObject(); | ||||||
|  |  | ||||||
|  |   // Add values in the object | ||||||
|  |   // | ||||||
|  |   // Most of the time, you can rely on the implicit casts. | ||||||
|  |   // In other case, you can do root.set<long>("time", 1351824120); | ||||||
|   root["sensor"] = "gps"; |   root["sensor"] = "gps"; | ||||||
|   root["time"] = 1351824120; |   root["time"] = 1351824120; | ||||||
|  |  | ||||||
|  |   // Add a nested array. | ||||||
|  |   // | ||||||
|  |   // It's also possible to create the array separately and add it to the | ||||||
|  |   // JsonObject but it's less efficient. | ||||||
|   JsonArray& data = root.createNestedArray("data"); |   JsonArray& data = root.createNestedArray("data"); | ||||||
|   data.add(double_with_n_digits(48.756080, 6)); |   data.add(double_with_n_digits(48.756080, 6)); | ||||||
|   data.add(double_with_n_digits(2.302038, 6)); |   data.add(double_with_n_digits(2.302038, 6)); | ||||||
|   | |||||||
| @@ -13,23 +13,49 @@ void setup() { | |||||||
|     // wait serial port initialization |     // wait serial port initialization | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   // Memory pool for JSON object tree. | ||||||
|  |   // | ||||||
|  |   // Inside the brackets, 200 is the size of the pool in bytes, | ||||||
|  |   // If the JSON object is more complex, you need to increase that value. | ||||||
|   StaticJsonBuffer<200> jsonBuffer; |   StaticJsonBuffer<200> jsonBuffer; | ||||||
|  |  | ||||||
|  |   // StaticJsonBuffer allocates memory on the stack, it can be | ||||||
|  |   // replaced by DynamicJsonBuffer which allocates in the heap. | ||||||
|  |   // It's simpler but less efficient. | ||||||
|  |   // | ||||||
|  |   // DynamicJsonBuffer  jsonBuffer; | ||||||
|  |  | ||||||
|  |   // 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. | ||||||
|   char json[] = |   char json[] = | ||||||
|       "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; |       "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; | ||||||
|  |  | ||||||
|  |   // Root of the object tree. | ||||||
|  |   // | ||||||
|  |   // It's a reference to the JsonObject, the actual bytes are inside the | ||||||
|  |   // JsonBuffer with all the other nodes of the object tree. | ||||||
|  |   // Memory is freed when jsonBuffer goes out of scope. | ||||||
|   JsonObject& root = jsonBuffer.parseObject(json); |   JsonObject& root = jsonBuffer.parseObject(json); | ||||||
|  |  | ||||||
|  |   // Test if parsing succeeds. | ||||||
|   if (!root.success()) { |   if (!root.success()) { | ||||||
|     Serial.println("parseObject() failed"); |     Serial.println("parseObject() failed"); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   // Fetch values. | ||||||
|  |   // | ||||||
|  |   // Most of the time, you can rely on the implicit casts. | ||||||
|  |   // In other case, you can do root["time"].as<long>(); | ||||||
|   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]; | ||||||
|  |  | ||||||
|  |   // Print values. | ||||||
|   Serial.println(sensor); |   Serial.println(sensor); | ||||||
|   Serial.println(time); |   Serial.println(time); | ||||||
|   Serial.println(latitude, 6); |   Serial.println(latitude, 6); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user