Click here to Skip to main content
14,882,311 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to deserialize this JSON and get name,issues,totalCount in the Serial monitor.But I get only 0 as the output...

JavaScript
{
"name": "ArduinoJson",
"stargazers": {
"totalCount": 3415
},
"issues": {
"totalCount": 21
}
}


What is the error in my code?

What I have tried:

C++
#include <ArduinoJson.h>
DynamicJsonDocument doc(2000);
String j;

const char* name = doc["name"];
long stars = doc["stargazers"]["totalCount"];
int issues = doc["issues"]["totalCount"];

void setup() {
  Serial.begin(9600);
}

void loop() {
 if (Serial.available()){
  j=Serial.readString();
   deserializeJson(doc,j);
 }

Serial.println(name);   
Serial.println(stars);
Serial.println(issues);

 delay(1000);

}
Posted
Updated 21-Dec-20 8:22am

I'm not an expert, but...
Form this library sample code JsonParserExample.ino | ArduinoJson 6[^] it is apparent that first you must deserialize and then you can fetch the values (e.g. stars = doc["stargazers"]["totalCount"];).
The sample code also shows you how to handle deserialization errors.
   
Comments
Member 13358124 18-Feb-20 7:54am
   
Thank you...
CPallini 18-Feb-20 8:07am
   
You are welcome.
Serial.println( doc["name"]);
Serial.println( doc["stargazers"]["totalCount"]);
Serial.println( doc["issues"]["totalCount"]);
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900