| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2014-11-03 21:29:55 +01:00
										 |  |  | // MIT License
 | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  | //
 | 
					
						
							|  |  |  | // This example shows how to deserialize a JSON document with ArduinoJson.
 | 
					
						
							| 
									
										
										
										
											2019-03-04 12:17:41 +01:00
										 |  |  | //
 | 
					
						
							|  |  |  | // https://arduinojson.org/v6/example/parser/
 | 
					
						
							| 
									
										
										
										
											2014-03-01 12:59:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-03 21:29:55 +01:00
										 |  |  | #include <ArduinoJson.h>
 | 
					
						
							| 
									
										
										
										
											2014-03-01 12:59:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-03 21:29:55 +01:00
										 |  |  | void setup() { | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  |   // Initialize serial port
 | 
					
						
							| 
									
										
										
										
											2014-11-03 21:29:55 +01:00
										 |  |  |   Serial.begin(9600); | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  |   while (!Serial) continue; | 
					
						
							| 
									
										
										
										
											2014-07-03 14:00:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-07 11:21:54 +02:00
										 |  |  |   // Allocate the JSON document
 | 
					
						
							| 
									
										
										
										
											2016-02-01 13:31:07 +01:00
										 |  |  |   //
 | 
					
						
							| 
									
										
										
										
											2019-01-29 14:09:09 +01:00
										 |  |  |   // Inside the brackets, 200 is the capacity of the memory pool in bytes.
 | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  |   // Don't forget to change this value to match your JSON document.
 | 
					
						
							| 
									
										
										
										
											2021-03-29 17:14:01 +02:00
										 |  |  |   // Use https://arduinojson.org/v6/assistant to compute the capacity.
 | 
					
						
							| 
									
										
										
										
											2018-04-17 21:27:45 +02:00
										 |  |  |   StaticJsonDocument<200> doc; | 
					
						
							| 
									
										
										
										
											2014-03-01 12:59:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-17 21:27:45 +02:00
										 |  |  |   // StaticJsonDocument<N> allocates memory on the stack, it can be
 | 
					
						
							| 
									
										
										
										
											2018-10-25 20:21:23 +08:00
										 |  |  |   // replaced by DynamicJsonDocument which allocates in the heap.
 | 
					
						
							| 
									
										
										
										
											2016-02-01 13:31:07 +01:00
										 |  |  |   //
 | 
					
						
							| 
									
										
										
										
											2018-10-25 20:21:23 +08:00
										 |  |  |   // DynamicJsonDocument doc(200);
 | 
					
						
							| 
									
										
										
										
											2016-02-01 13:31:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // JSON input string.
 | 
					
						
							|  |  |  |   //
 | 
					
						
							| 
									
										
										
										
											2019-01-29 14:09:09 +01:00
										 |  |  |   // 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.
 | 
					
						
							| 
									
										
										
										
											2014-11-03 21:29:55 +01:00
										 |  |  |   char json[] = | 
					
						
							|  |  |  |       "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; | 
					
						
							| 
									
										
										
										
											2014-03-01 12:59:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-17 21:27:45 +02:00
										 |  |  |   // Deserialize the JSON document
 | 
					
						
							| 
									
										
										
										
											2018-05-15 18:23:09 +02:00
										 |  |  |   DeserializationError error = deserializeJson(doc, json); | 
					
						
							| 
									
										
										
										
											2014-03-01 12:59:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 13:31:07 +01:00
										 |  |  |   // Test if parsing succeeds.
 | 
					
						
							| 
									
										
										
										
											2018-03-09 16:58:01 +01:00
										 |  |  |   if (error) { | 
					
						
							| 
									
										
										
										
											2018-04-17 21:27:45 +02:00
										 |  |  |     Serial.print(F("deserializeJson() failed: ")); | 
					
						
							| 
									
										
										
										
											2020-09-14 09:30:58 +02:00
										 |  |  |     Serial.println(error.f_str()); | 
					
						
							| 
									
										
										
										
											2014-11-03 21:29:55 +01:00
										 |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-03-01 12:59:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 13:31:07 +01:00
										 |  |  |   // Fetch values.
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   // Most of the time, you can rely on the implicit casts.
 | 
					
						
							| 
									
										
										
										
											2019-01-29 14:09:09 +01:00
										 |  |  |   // In other case, you can do doc["time"].as<long>();
 | 
					
						
							|  |  |  |   const char* sensor = doc["sensor"]; | 
					
						
							|  |  |  |   long time = doc["time"]; | 
					
						
							|  |  |  |   double latitude = doc["data"][0]; | 
					
						
							|  |  |  |   double longitude = doc["data"][1]; | 
					
						
							| 
									
										
										
										
											2014-03-01 12:59:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 13:31:07 +01:00
										 |  |  |   // Print values.
 | 
					
						
							| 
									
										
										
										
											2014-11-03 21:29:55 +01:00
										 |  |  |   Serial.println(sensor); | 
					
						
							|  |  |  |   Serial.println(time); | 
					
						
							|  |  |  |   Serial.println(latitude, 6); | 
					
						
							|  |  |  |   Serial.println(longitude, 6); | 
					
						
							| 
									
										
										
										
											2014-03-01 12:59:45 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-03 21:29:55 +01:00
										 |  |  | void loop() { | 
					
						
							|  |  |  |   // not used in this example
 | 
					
						
							| 
									
										
										
										
											2015-08-20 15:15:59 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | // deserialization problem.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // The book "Mastering ArduinoJson" contains a tutorial on deserialization.
 | 
					
						
							|  |  |  | // It begins with a simple example, like the one above, and then adds more
 | 
					
						
							|  |  |  | // features like deserializing directly from a file or an HTTP request.
 | 
					
						
							|  |  |  | // Learn more at https://arduinojson.org/book/
 | 
					
						
							|  |  |  | // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
 |