0

我有以下JSON字符串

{ "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}] }

虽然使用 libJSON,但我能够获取: 的值 "PlayListName""CountOfFilesinPlayList"并且还可以识别"PlayListArray",但我无法找出如何提取"PlayListArray".

以下是解析JSON字符串的代码片段

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;                                        
    }
} 
4

1 回答 1

1

这应该会有所帮助 - http://linuxprograms.wordpress.com/2010/06/01/json_object_get_array/ 您可以使用以下代码

  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;
    }
  }
于 2012-12-13T10:14:15.013 回答