DaveGamble / cJSON

Ultralightweight JSON parser in ANSI C
MIT License
10.87k stars 3.22k forks source link

cJSON_Parse return NULL #541

Open Muhan1995 opened 3 years ago

Muhan1995 commented 3 years ago

Hello! Thank you so much for your work! I have a JSON parse problem. It returns NULL what could be the problem ??? The rest cJSON_CreateObject (), cJSON_AddStringToObject (), cJSON_AddItemToObject (), cJSON_Print () all work wonderfully !!! Here is some of the code: cJSON_Hooks memoryHook; memoryHook.malloc_fn = OS_Malloc; memoryHook.free_fn = OS_Free; cJSON_InitHooks (& memoryHook);

cJSON json_processing = cJSON_Parse (msg_json); if (json_processing! = NULL) { cJSON state = cJSON_GetObjectItem (json_processing, "state"); cJSON * desired = cJSON_GetObjectItem (state, "desired"); if (desired! = NULL) { .............................. } } else { Trace (1, "Error parse json"); } cJSON_Delete (json_processing);

My module: Al-thinker A9G SDK: GPRS_C_SDK-master IDE: CSDTK42 could there be a problem in the standard std libraries ??? I heard something is wrong. Link: https://github.com/Ai-Thinker-Open/GPRS_C_SDK/tree/master/include/std_inc I tried to install fresh versions CJSON. But it doesn't help!

Alanscut commented 3 years ago

Could your please offer the parsing error msg? You can get it with CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void) function

Muhan1995 commented 3 years ago

Here: cJSON *json_processing = cJSON_Parse(msg_json); Trace(1, "Infi error parse:"); Trace(1, cJSON_GetErrorPtr()); if(json_processing != NULL) { Trace(1,"Parse json ok"); ........................... } else { Trace(1,"Error parse json"); } cJSON_Delete(json_processing);

the function returns nothing

Screen: image

Muhan1995 commented 3 years ago

Found the reason. The problem went away when the json was done this way: char* msg_json = "{ \"state\": { \"desired\": { \"EjectPowerBank\": \"3\" } } }"; So the problem really was in the standard library. I think that he could not parse int because of this it gave a NULL result. Now I'm fixing standard libraries. Then I'll tell you how it turned out to fix. Thanks everyone!!!

Muhan1995 commented 3 years ago

After such alterations, everything worked. The problem with the strtod () function was that it was not correctly implemented in the SDK. I replaced it with the atof () function. image

bhsshh commented 3 months ago

After such alterations, everything worked. The problem with the strtod () function was that it was not correctly implemented in the SDK. I replaced it with the atof () function. image

Oh my god! I solved the same problem using your method, thank you very much!