1

I am using json parser lib written in C to parse JSON objects. The lib link is : https://github.com/udp/json-parser.

The json object/string, which I am trying to parse is :

{"video_id": 105, "st": "S3", "processing" : [{"start" : "1", "end" : "2"}]}}

"processing" contains another JSON object.

I have parsed the first three items. But I am not able to figure out a way to parse the "processing" json object. I am using following code:-

   if (!strcmp(json->u.object.values[i].name, "video_id")) 
    {
      video_id=json->u.object.values[i].value->u.integer;
    }
    .
    .
    if (!strcmp(json->u.object.values[i].name, "processing")) 
    {
         printf("\nNumber of JSON OBJECTS : %d\n", json->u.object.values[i].value->u.object.length); 
    }

json is the parsed object obtained via calling the lib on the JSON string. Can anyone guide me how to handle the nested object ?

Any help will be really appreciated

My complete code is :

json_value *json;                                                   
json_char *json_object="{\"video_id\": 105, \"st\": \"S3\", \"processing\" : [{\"type\" : \"clipping\"},{\"fese\" : \"dipping\"}]}";



    printf("%s",json_object);
    //json_value * json_parse (const json_char * json,
      //                   size_t length);
    json=json_parse(json_object, strlen(json_object));

//  json_type json_object;


    printf("\n%s\n",json->u.object.values[0].name);
    printf("\t%d\n",json->u.object.values[0].value->u.integer);

    printf("\n%s\n",json->u.object.values[2].name);
    printf("\t%d\n",json->u.object.values[2].value->u.object.length);

printf("\t%s\n",json->u.object.values[2].value->u.object.values[0].name);
0

2 Answers 2

1

From the documentation, API field::

The type field of json_value is one of:

  • json_object (see u.object.length, u.object.values[x].name, u.object.values[x].value)
  • json_array (see u.array.length, u.array.values)
  • json_integer (see u.integer)
  • json_double (see u.dbl)
  • json_string (see u.string.ptr, u.string.length)
  • json_boolean (see u.boolean)
  • json_null

So, check the type field of the "processing" value. If found to be json_array, do a json_parse for the array to get a new json_value. Now this json_value will provide you with the nested JSON objects.

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

3 Comments

HI. . u mean that I should check IF (json->u.object.values[i].type==JSON_OBJECT), then parse that again by using json_parse function. Plz correct me if I am wrong . . .
Can you post the structure for json_value? I think you have to check if( json->u.object.values[i].type == json_array ) and then again parse using json_parse.
hi, I have provided code in my question, which I am using just to read values of nested structure. But no luck so far. . Plz have a look and let me know. So I can experiment with the new stuff. It tell me exactly that there are two nested JSON objects but I cannot figure out a way to read them .
0

Take these as reference:

js_v->u.object.values[1].value->u.array.values[0]->type
js_v->u.object.values[1].value->u.array.values[0]->u.string.ptr

I've used them to reference to a string element inside an array:
{"t":"type","d":["element1","element2","element3"]}

In your case, I think you should to repeat the structure like this:
js_v->u.object.values[2].value->u.array.values[0]->u.object.values[0]->u.string.ptr

luck!

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.