1

I have following JSON string

{ "PlayListName":"PlayList1","CountOfFilesinPlayList":5, "PlayListArray":[ {"DevId":1,"FileId":1,"Title":"This is a test %% @_1_1","m_iFileType":0},{"DevId":1,"FileId":2,"Title":"This is a test %% @_1_2","m_iFileType":1},{"DevId":2,"FileId":3,"Title":"This is a test %% @_2_3","m_iFileType":0},{"DevId":2,"FileId":4,"Title":"This is a test %% @_2_4","m_iFileType":2},{"DevId":3,"FileId":5,"Title":"This is a test %% @_3_5","FileType":0}] }

Although using libJSON, I am able to get the values of: "PlayListName", "CountOfFilesinPlayList" and also identify "PlayListArray", I am not able to find out how to extract the content of "PlayListArray".

Following is the code snippet which is parsing the JSON string

int_n nCountOfFiles;
PlayList_st *pPlaylistArr;
int8_n szPlayListName[PLAYLIST_NAME_LENGTH];

json_object *new_obj;
enum json_type type;

new_obj = json_tokener_parse\
    ((char *)args[0].value.stringValue.UTF8Characters);

if(!new_obj) {
    result->type = NPVariantType_Bool;
    result->value.boolValue = false;
    return false;
}
json_object_object_foreach(new_obj, key, val) {
    type = json_object_get_type(val);
        switch(type) {
        case json_type_int:
            if(key && !strcmp((const char *)key, "CountOfFilesinPlayList")) 
            {
                nCountOfFiles = json_object_get_int(val);
                /* Allocate pPlaylistName array */
                if(nCountOfFiles > 0)
                {
                    .....
                    .....
                }
                else
                {                   
                    .....
                    .....                        
                }
            } 
            break;
        case json_type_string:
            if(key && !strcmp((const char *)key, "PlayListName")) 
            {                   
                strncpy (szPlayListName, json_object_get_string(val), \
                    PLAYLIST_NAME_LENGTH-1);

            }
            break;
        case json_type_array:
            if(key && !strcmp((const char *)key, "PlayListArray")) 
            {
                MEDIA_DEBUG_PRINT("\nwcf Media plugin: Found PlayListArray");
            }
            break;
        default:
                    result->type = NPVariantType_Bool;
                    result->value.boolValue = false;
                    return false;                                        
    }
} 
0

2 Answers 2

2

This should help - http://linuxprograms.wordpress.com/2010/06/01/json_object_get_array/ You can use the following code

  json_object_object_foreach(jobj, key, val) {
    type = json_object_get_type(val);
    switch (type) {
      case json_type_array: printf("type: json_type_array, ");
                          jobj = json_object_object_get(jobj, key);
                          int arraylen = json_object_array_length(jobj);
                          printf("Array Length: %dn",arraylen);
                          int i;
                          json_object * jvalue;
                          for (i=0; i< arraylen; i++){
                            jvalue = json_object_array_get_idx(jobj, i);
                            printf("value[%d]: %sn",i, json_object_get_string(jvalue));
                          }
                          break;
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

0

The easier way: Assuming your json is in j:

cJSON *j;  // Your array
cJSON *jA; // The Playlist

// playlist Array is the “child”

jA = j->child;

jA->child is first item in Array
jA->child->next is next item in Array
...

If you know the structure of the array it'ss easy to parse without having to do all the type checking. I’m using a 10 year old version with many of my own additions and I tend not to like the murky ForEach and parse function in the “library"

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.