色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

c json三層嵌套

李中冰1年前8瀏覽0評論

C JSON三層嵌套

在使用C語言進行JSON數據處理時,經常需要處理多重嵌套的結構體,尤其是在處理復雜的JSON數據結構時,會遇到三層及以上的嵌套。以下是通過三層嵌套的JSON數據結構進行C語言處理的具體方法。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <json-c/json.h>
const char *json_input = "{\"name\":\"Tom\",\
\"age\": 20,\
\"interests\": {\"music\": [{\"name\": \"pop\",\"style\": \"modern\"}, {\"name\": \"classical\",\"style\": \"traditional\"}],\"book\": [{\"name\": \"novel\",\"style\": \"fiction\"}, {\"name\": \"history\",\"style\": \"nonfiction\"}]}\
}";
int main()
{
struct json_object *root_obj, *interests_obj, *music_array, *book_array, *music_item, *book_item, *name_obj, *style_obj;
const char *name, *style;
int i, j;
root_obj = json_tokener_parse(json_input);
json_object_object_get_ex(root_obj, "interests", &interests_obj);
json_object_object_get_ex(interests_obj, "music", &music_array);
for (i = 0; i< json_object_array_length(music_array); i++)
{
music_item = json_object_array_get_idx(music_array, i);
json_object_object_get_ex(music_item, "name", &name_obj);
name = json_object_get_string(name_obj);
printf("music[%d].name: %s\n", i, name);
json_object_object_get_ex(music_item, "style", &style_obj);
style = json_object_get_string(style_obj);
printf("music[%d].style: %s\n", i, style);
}
json_object_object_get_ex(interests_obj, "book", &book_array);
for (j = 0; j< json_object_array_length(book_array); j++)
{
book_item = json_object_array_get_idx(book_array, j);
json_object_object_get_ex(book_item, "name", &name_obj);
name = json_object_get_string(name_obj);
printf("book[%d].name: %s\n", j, name);
json_object_object_get_ex(book_item, "style", &style_obj);
style = json_object_get_string(style_obj);
printf("book[%d].style: %s\n", j, style);
}
json_object_put(root_obj);
return 0;
}

運行該程序將輸出如下JSON數據結構中音樂與圖書的名稱和風格:

music[0].name: pop
music[0].style: modern
music[1].name: classical
music[1].style: traditional
book[0].name: novel
book[0].style: fiction
book[1].name: history
book[1].style: nonfiction

以上是C語言處理JSON三層嵌套結構的基本處理方法,可以根據實際需求進行修改和擴展。