json嵌套数组-第11讲
Posted Linux编程学堂
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json嵌套数组-第11讲相关的知识,希望对你有一定的参考价值。
假设有如下的 JSON 数据:
"reader_list": [
"ID": 196,
"name": "reader2",
"borrow_book_id": [169]
,
"ID": 198,
"name": "reader1",
"borrow_book_id": [166, 169]
]
那么,reader_list 是一个数组,数组的每一个元素中,又有 brrow_book_id 是数组;
所以,创建该 JSON 的代码如下:
韦凯峰 Linux C/C++零基础编程教程
Linux系统编程,Openwrt系统开发
//========================================
//========================================
char* reader_downlaod(void)
struct list_node* plist = NULL;
char* pbuf = NULL;
cJSON *root = cJSON_CreateObject();
cJSON *array = cJSON_CreateArray();
cJSON_AddItemToObject(root, "reader_list", array);
//====================
//遍历链表
//====================
list_for_each(plist, &reader_list)
if(plist == NULL)
LOG("===== reader_list end =====\\n");
break;
struct node_reader_info* pnode = list_entry(plist, struct node_reader_info, list);
if(pnode == NULL)
LOG("===== reader_list end 1 =====\\n");
break;
//===================
//===================
#if 0
reader_info_print(pnode->pr);
#endif
//===================
//构建数据存储到 JSON 结构中;
//===================
#if 1
if(pnode->pr)
cJSON *obj = cJSON_CreateObject();
cJSON_AddItemToArray(array, obj);
cJSON_AddItemToObject(obj, "ID", cJSON_CreateNumber(pnode->pr->ID));
cJSON_AddItemToObject(obj, "name", cJSON_CreateString(pnode->pr->name));
//===================
//创建读者借阅图书的数组;
//===================
cJSON *borrow_book_array = cJSON_CreateArray();
cJSON_AddItemToObject(obj, "borrow_book_id", borrow_book_array);
int i;
for(i = 0; i < 32; i++)
if(pnode->pr->borrow_book_id[i] <= 0)
continue;
cJSON_AddItemToArray(borrow_book_array, cJSON_CreateNumber(pnode->pr->borrow_book_id[i]));
//end for
#endif
//end for
LOG("=== end ===\\n");
pbuf = cJSON_Print(root);
//===================
//===================
#if 1
if(pbuf)
LOG("pbuf = %s\\n", pbuf);
#endif
cJSON_Delete(root); //释放对象
//===================
//这里返回内存块, 在函数外面使用完内存块之后再释放;
//===================
//free(pbuf); //需要释放内存;
return pbuf;
韦凯峰 Linux C/C++零基础编程教程
Linux系统编程,Openwrt系统开发
//========================================================================
可以看到, 在创建子数组的时候,是:
//===================
//创建读者借阅图书的数组;
//===================
cJSON *borrow_book_array = cJSON_CreateArray();
cJSON_AddItemToObject(obj, "borrow_book_id", borrow_book_array);
int i;
for(i = 0; i < 32; i++)
if(pnode->pr->borrow_book_id[i] <= 0)
continue;
cJSON_AddItemToArray(borrow_book_array, cJSON_CreateNumber(pnode->pr->borrow_book_id[i]));
//end for
此时,创建一个数组对象 borrow_book_array,插入奥 obj 对象中;
然后,给数组对象 borrow_book_array 添加元素,如下:
cJSON_AddItemToArray(borrow_book_array, cJSON_CreateNumber(pnode->pr->borrow_book_id[i]));
//========================================================================
那么,解析该 JSON 的代码如下:
//========================================
//下载读者信息;
//========================================
int reader_download(struct book_manager* pbm)
int ret, i;
ret = http_pkg_send_no_body(&(pbm->sock), HTTP_METHOD_READER_DOWNLOAD, NULL, 0);
if(http_pkg_get_response(&pbm->sock, pbm->http_pkg_response, MAX_PKG_LEN) < 0)
goto err;
//==================
//==================
int status = http_pkg_header_get_status(pbm->http_pkg_response);
if(status)
goto err;
LOG("=== download reader ok ===\\n");
#if 0
char head[MAX_PKG_LEN] = 0;
http_pkg_get_header(pbm->http_pkg_response, head, MAX_PKG_LEN);
#endif
int len = http_header_content_length(pbm->http_pkg_response);
if(len < 0)
LOGE("pkg not body!\\n");
goto err;
char body[MAX_PKG_LEN] = 0;
http_pkg_get_body(pbm->http_pkg_response, body, MAX_PKG_LEN);
//==================
//解析 json 数据;
//==================
cJSON *json = NULL;
json = cJSON_Parse(body);
if(!json)
LOGE("parse body err!\\n");
goto err;
cJSON* books = cJSON_GetObjectItem(json, "reader_list");
int books_num = cJSON_GetArraySize(books);
cJSON* item = NULL;
cJSON* obj = NULL;
cJSON* it1 = NULL;
//===================
//===================
for(i = 0; i < books_num; i++)
item = cJSON_GetArrayItem(books, i);
obj = cJSON_GetObjectItem(item, "ID");
LOG("ID = %d\\n", obj->valueint);
obj = cJSON_GetObjectItem(item, "name");
LOG("name = %s\\n", obj->valuestring);
//===================
//获取借阅的图书
//===================
obj = cJSON_GetObjectItem(item, "borrow_book_id");
int borrow_book_num = cJSON_GetArraySize(obj);
LOG("borrow_book_num = %d\\n", borrow_book_num);
int j;
for(j = 0; j < borrow_book_num; j++)
it1 = cJSON_GetArrayItem(obj, j);
LOG("borrow_book_id[%d] = %d\\n", j, it1->valueint);
//end for
//end for
cJSON_Delete(json); //释放对象
return 0;
err:
LOG("=== download book err ===\\n");
return 1;
韦凯峰 Linux C/C++零基础编程教程
Linux系统编程,Openwrt系统开发
以上是关于json嵌套数组-第11讲的主要内容,如果未能解决你的问题,请参考以下文章