1

First off, this is a very broad question, and it might come across as me asking for the community to write my code for me. That is not my intent, but I am so lost, I don't know how to give enough information.

I am attempting to use the cJSON library, written by Dave Gamble, I found this is very useful to use for my embedded device for JSON parse and composing.

to read in the following JSON array

{ 
 "name": "Jack", 
  "types":[23,56,78],
 "format": {
 "type": "rect",
  "width": 1920, } 
}

.. and parsing the getting the object worked with this method

  cJSON *format = cJSON_GetObjectItem(json,"format");

  int framerate = cJSON_GetObjectItem(format,"width")->valueint; 

but I am not able to parse the key "name" and object simple key value ,

I tried this

  cJSON *array = cJSON_GetArrayItem(json,"types"); 

  int value = cJSON_GetArrayItem(format1,1)->valueint;

but did not work, how to parse the array object and simple key value..

3
  • Try using cJSON *array = cJSON_GetObjectItem(json,"types") to get the array. Commented Apr 29, 2014 at 16:15
  • hi thanks for it , it works ... and how do I read just simple string:value ? in my example I wanna to read "name": "Jack" .. Commented Apr 30, 2014 at 4:25
  • ganimede.ro/help/abljson/files/jsonParser-p.html#getStringValue Commented Apr 30, 2014 at 4:56

2 Answers 2

2

Your json does not respect the key:value format as pointed by @lxgeek. Still, you can iterate through array of values in cJSON:

cJSON * array = cJSON_GetObjectItem(json, "types");
for (i = 0 ; i < cJSON_GetArraySize(array) ; i++)
{
    printf("%d ",cJSON_GetArrayItem(array, i)->valueint);
}

will print

23 56 78
Sign up to request clarification or add additional context in comments.

Comments

1

I think JSON element should respect key:value format.

{ 
 "name": "Jack", 
  "types":[{"type" : 23}, {"type" : 56}, {"type":78}],
 "format": {
 "type": "rect",
  "width": 1920, } 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.