我有以下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;
}
}
发布于 2012-12-13 10:14:15
这应该会有所帮助- 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;
}
}
https://stackoverflow.com/questions/12175269
复制相似问题