I developing a C embedded software that build a JSON string with several parameters ( string and integer ). For my purpose I used cJSON ) in future I need to parse complex JSON).
I build my json with this code:
jsonObject = cJSON_CreateObject();
cJSON_AddNumberToObject( jsonObject, "Version", 1 );
cJSON_AddStringToObject( jsonObject, "ID", "xyz" );
cJSON_AddStringToObject( jsonObject, "Local", "1234" );
cJSON_AddNumberToObject( jsonObject, "Type", 1 );
cJSON_AddStringToObject( jsonObject, "Message", "Hello" );
Next I print the JSON into a dynamic buffer using:
cJSON_PrintPreallocated( jsonObject, *jsonMessage, *jsonMessageLen, 0 )
When I visualise the jsonMessage on a terminal, the property "Type" hasn't a value.
This is the built json:
{"Version":1,"ID":"xyz","Local":"123","Type":,"Message":"Hello"}
I try to use a different print method, for example I used:
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt)
And the JSON is built correctly. The problem is that I need to allocate the memory by myself and not using cJSON_PrintBuffered. This because I cannot use malloc and free function.
Any suggestions?
Thanks