/* ArduinoJson - https://arduinojson.org
This example shows how to deserialize a JSON document with ArduinoJson.
Section 3.2 of ArduinoJSON book
See VSCode C++ 3.2_deserializing.cpp for C++ version
8/5/25 CM:
Working!!!
8/7/25 CM:
Not working exactly as C++ verson.
Tried on EPS8266 and received the same output as on Wokwi.
*/
#include <ArduinoJson.h>
void setup() {
Serial.begin(9600);
JsonDocument myDoc; // Allocate the JSON document
// JSON input string.
//const char* json =
// "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
// When you have a line break you must end a line with " and start the next line with "
const char* input =
"{\"name\":\"ArduinoJson\", \"stargazers\":{\"totalCount\":6287}, \"issues\":{\"totalCount\":22}, \"myNum\":1234}";
/* // This works too!
const char* input =
"{\"name\":\"ArduinoJson\","
"\"stargazers\":{\"totalCount\":6287},"
"\"issues\":{\"totalCount\":22},"
"\"myNum\":1234}";
*/
// Deserialize the JSON document
DeserializationError error = deserializeJson(myDoc, input);
// Test if parsing succeeds
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str()); // works with error.c_str() as well
}
else {
Serial.print(F("deserializeJson() passed: "));
Serial.println(error.f_str()); // works with error.c_str() as well
}
//Serial.print("myDoc: ");
//Serial.println(myDoc); // Error message on this line, won't print 'myDoc'
// Fetch the values
// Most of the time, you can rely on the implicit casts.
// In other case, you can do doc["time"].as<long>();
const char* name = myDoc["name"];
String name2 = myDoc["name"]; // alternative way to display a string
int stars = myDoc["stargazers"]["totalCount"];
uint16_t stars2 = myDoc["stargazers"]["totalCount"];
uint8_t issues = myDoc["issues"]["totalCount"];
unsigned char issues2 = myDoc["issues"]["totalCount"];
short issues3 = myDoc["issues"]["totalCount"];
// Using '|' OR if the value returns null or '0'
uint16_t myNum = myDoc["myNum"] | 80;
// Print the values
Serial.println(name);
Serial.println(name2);
Serial.println(stars);
Serial.println(stars2);
Serial.println(issues);
Serial.println(issues2);
Serial.println(issues3);
Serial.println(myNum);
/*
>>>>>>>>>> JsonObject <<<<<<<<<<
Now that we have a JsonObject, we can look at all the keys and their associated values. In ArduinoJson, a key-value pair is represented by the JsonPair class.
We can enumerate all pairs with a simple for loop:
JsonObject is similar to a smart pointer. It wraps a pointer with a class that is easy to use.
When you modify the JsonObject, you also alter the JsonDocument
*/
JsonObject myObject = myDoc.as<JsonObject>();
// Now that we have a JsonObject, we can look at all the keys and their associated values.
// In ArduinoJson, a key-value pair is represented by the JsonPair class.
// Loop through all the key-value pairs in myObject...
for (JsonPair p : myObject) {
p.key(); // is a JsonString
p.value(); // is a JsonVariant
// and determine... Is it a string?
if (p.value().is <const char*> ()) { // Can use 'string' in place of 'const char*'
const char* isString = p.value(); // We can get the value via implicit cast:
Serial.print("Value is string: "); Serial.println(isString);
}
else if (p.value().is <JsonObject> ()) {
JsonObject isObject = p.value();
Serial.print("Value is JsonObject: "); Serial.println(isObject);
}
else if (p.value().is <int> ()) {
int isInt = p.value();
Serial.print("Value is Integer: "); Serial.println(isInt);
}
else {
Serial.println("Not a string or integer");
}
}
}
void loop() {
// not used in this example
}