-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot Parse JSON #828
Comments
What version of cJSON are you using? I tried your JSON object as shown and |
I am using ESP-IDFs cJSON, so I guess it is the last version? I don't know, and it seems I can't check it either. EDIT: I tried with a much simpler script and JSON, and yet again the root object is null. void parseJSON(char *json)
{
cJSON *root = cJSON_Parse(json);
if (root == NULL)
{
const char *error_ptr = cJSON_GetErrorPtr();
printf("Error: %s\n", error_ptr);
}
char *value = cJSON_GetObjectItemCaseSensitive(root, "key")->valuestring;
printf("JSON KEY: %s\n", value);
} {"key": "value"} The cJSON_GetErrorPtr() output is:
|
It's interesting you are seeing a "core panic" since you are able to print an error message after the parser returns NULL. If you are trying to use the NULL value later, that could be reason for the core panic. I was hoping you could also show the actual string you are passing to the JSON parser. |
I think the core panic occurs when calling the void parseJSON(char *json)
{
printf("%s\n", json); // Prints {'key': 'value'}␙�␑
cJSON *root = cJSON_Parse(json);
if (root == NULL)
{
const char *error_ptr = cJSON_GetErrorPtr();
printf("Error: %s\n", error_ptr); // Prints Error: key': 'value'}␙�␑
return;
}
char *value = cJSON_GetObjectItemCaseSensitive(root, "key")->valuestring;
printf("JSON KEY: %s\n", value);
} EDIT: I tried by embedding the JSON inside the code and it works! I think I figured out what the problem is. void parseJSON(char *json)
{
char *json2 = "{\"key\":\"value\"}";
printf("%s\n", json2); // Prints {"key":"value"}
cJSON *root = cJSON_Parse(json2);
if (root == NULL)
{
const char *error_ptr = cJSON_GetErrorPtr();
printf("Error: %s\n", error_ptr);
return;
}
char *value = cJSON_GetObjectItemCaseSensitive(root, "key")->valuestring;
printf("JSON KEY: %s\n", value); // Prints JSON KEY: value
} So apparently, the JSON is sent with single quotes |
Yes, specifically C uses single quotes for characters or extended characters, which internally are stored as |
Hello team! I am using cJSON in combination with ESP-IDF/FreeRTOS to parse the JSONs that arrive via MQTT, however, every time it receives a JSON, the program crashes because the root object is NULL, i.e., it cannot parse the JSON.
This is the JSON:
And this is the code:
This is the output of cJSON_GetErrorPtr():
Followed by:
The text was updated successfully, but these errors were encountered: