json多级嵌套对象创建-第9讲
Posted Linux编程学堂
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json多级嵌套对象创建-第9讲相关的知识,希望对你有一定的参考价值。
在网盘的用户管理中,我们创建如下的用户信息表:
[root@localhost64 httpd]# cat user_info.json
"users": [
"name": "admin",
"pwd": "123456",
"status": 1,
"ope_dir": []
,
"name": "test",
"pwd": "123456",
"status": 2,
"ope_dir": []
,
"name": "abc",
"pwd": "123456",
"status": 3,
"ope_dir": [
"dir": "/sda1/upload",
"power": 1
,
"dir": "/sda2/upload",
"power": 2
]
]
[root@localhost64 httpd]#
韦凯峰 Linux C/C++零基础编程教程
Linux系统编程,Openwrt系统开发
可以看到,users 是跟数组名,里面是每一个 user 用户的信息;
然后,在一个 user 用户信息里面,也存在 ope_dir 数组。这个数组是用户的操作目录;
操作目录数组的元素是:
"dir": "/sda1/upload",
"power": 1
所以,操作目录数组的元素,又提供了自己的属性名和属性值等等信息。
//========================================================================
下面是该 JSON 信息的参数的解析过程:
//========================================
//初始化用户集合;
//return 0 -- ok
//return 1 -- err
//========================================
int user_sets_init(struct user_manage_info* pu)
char* pbuf = NULL;
memset(pu, 0, sizeof(struct user_manage_info));
if(access(FN_USER_INFO, F_OK))
//====================
//文件不存在;
//====================
cJSON* root = NULL;
cJSON *array = NULL;
cJSON *obj = NULL;
cJSON *it1 = NULL;
cJSON *ope_dir = NULL;
root = cJSON_CreateObject();
if(!root)
LOGE(STR_ERR);
return 1;
array = cJSON_CreateArray();
cJSON_AddItemToObject(root, KU_USERS, array);
//====================
//注册默认管理员信息;
//====================
obj = cJSON_CreateObject();
cJSON_AddItemToArray(array, obj);
cJSON_AddItemToObject(obj, KU_NAME, cJSON_CreateString(DEF_ROOT_NAME));
cJSON_AddItemToObject(obj, KU_PWD, cJSON_CreateString(DEF_ROOT_PWD));
cJSON_AddItemToObject(obj, KU_STATUS, cJSON_CreateNumber(USER_STATUS_ROOT));
//操作目录;
ope_dir = cJSON_CreateArray();
cJSON_AddItemToObject(obj, KU_OPE_DIR, ope_dir);
//====================
//普通用户信息;
//====================
obj = cJSON_CreateObject();
cJSON_AddItemToArray(array, obj);
cJSON_AddItemToObject(obj, KU_NAME, cJSON_CreateString(DEF_NORMAL_USER_NAME));
cJSON_AddItemToObject(obj, KU_PWD, cJSON_CreateString(DEF_NORMAL_USER_PWD));
cJSON_AddItemToObject(obj, KU_STATUS, cJSON_CreateNumber(USER_STATUS_NORMAL));
//操作目录;
ope_dir = cJSON_CreateArray();
cJSON_AddItemToObject(obj, KU_OPE_DIR, ope_dir);
//====================
//操作指定目录的用户信息;
//====================
obj = cJSON_CreateObject();
cJSON_AddItemToArray(array, obj);
cJSON_AddItemToObject(obj, KU_NAME, cJSON_CreateString(DEF_USER_DIR_NAME));
cJSON_AddItemToObject(obj, KU_PWD, cJSON_CreateString(DEF_USER_DIR_PWD));
cJSON_AddItemToObject(obj, KU_STATUS, cJSON_CreateNumber(USER_STATUS_DIR_OPE));
//操作目录;
ope_dir = cJSON_CreateArray();
cJSON_AddItemToObject(obj, KU_OPE_DIR, ope_dir);
//====================
//构建元素, 添加到 ope_dir 数组中;
//====================
it1 = cJSON_CreateObject();
cJSON_AddItemToArray(ope_dir, it1);
cJSON_AddItemToObject(it1, KU_DIR, cJSON_CreateString(DEF_USER_DIR1_PATH));
cJSON_AddItemToObject(it1, KU_POWER, cJSON_CreateNumber(DEF_USER_DIR1_POWER));
it1 = cJSON_CreateObject();
cJSON_AddItemToArray(ope_dir, it1);
cJSON_AddItemToObject(it1, KU_DIR, cJSON_CreateString(DEF_USER_DIR2_PATH));
cJSON_AddItemToObject(it1, KU_POWER, cJSON_CreateNumber(DEF_USER_DIR2_POWER));
pbuf = cJSON_Print(root);
if(!pbuf)
LOGE(STR_ERR);
cJSON_Delete(root);
return 1;
//====================
//创建配置文件, 保存配置信息;
//====================
file_write_clear(FN_USER_INFO, pbuf, strlen(pbuf));
cJSON_Delete(root);
else
//====================
//配置文件存在;
//====================
pbuf = file_get_content(FN_USER_INFO);
if(!pbuf)
LOGE(STR_ERR);
return 1;
#if 0
LOG("pbuf str = %s\\n", pbuf);
#endif
//====================
//解析配置信息到 ps 对象中;
//====================
int ret = user_sets_parse(pu, pbuf);
free(pbuf);
//====================
//====================
#if 1
user_sets_print(pu);
#endif
return ret;
//========================================================================
//========================================
//解析用户信息;
//return 0 -- ok
//return 1 -- err
//========================================
int user_sets_parse(struct user_manage_info* pu, char* pstr)
#if 0
LOG("parse conf = %s\\n", pstr);
#endif
cJSON* json = NULL;
json = cJSON_Parse(pstr);
if (!json)
LOGE(STR_ERR);
return 1;
cJSON* users = cJSON_GetObjectItem(json, KU_USERS);
int user_num = cJSON_GetArraySize(users);
if(user_num <= 0)
LOGE("not user define!\\n");
return 1;
//================
//显示释放原来的资源;
//================
user_sets_clear(pu);
//================
//根据用户的数量, 申请内存;
//================
pu->psets = (struct user_info*)malloc(sizeof(struct user_info) * user_num);
memset(pu->psets, 0, sizeof(struct user_info) * user_num);
pu->num = user_num; //记录用户的数量;
//================
//================
cJSON* item = NULL;
cJSON* obj = NULL;
cJSON* ope_dir = NULL;
cJSON* it1 = NULL;
int i, j;
//================
//================
for(i = 0; i < user_num; i++)
item = cJSON_GetArrayItem(users, i);
obj = cJSON_GetObjectItem(item, KU_NAME);
strcpy(pu->psets[i].name, obj->valuestring);
obj = cJSON_GetObjectItem(item, KU_PWD);
strcpy(pu->psets[i].pwd, obj->valuestring);
obj = cJSON_GetObjectItem(item, KU_STATUS);
pu->psets[i].status = obj->valueint;
//===================
//用户操作的目录;
//===================
ope_dir = cJSON_GetObjectItem(item, KU_OPE_DIR);
int ope_dir_num = cJSON_GetArraySize(ope_dir);
pu->psets[i].ope_dir_num = ope_dir_num; //记录操作目录的个数;
LOG("ope_dir_num = %d\\n", ope_dir_num);
if(ope_dir_num > 0)
//申请内存, 解析操作目录数组;
pu->psets[i].ope_dir = (struct ope_dir_info*)malloc(sizeof(struct ope_dir_info)*ope_dir_num);
for(j = 0; j < ope_dir_num; j++)
item = cJSON_GetArrayItem(ope_dir, j);
obj = cJSON_GetObjectItem(item, KU_DIR);
strcpy(pu->psets[i].ope_dir[j].path, obj->valuestring);
obj = cJSON_GetObjectItem(item, KU_POWER);
pu->psets[i].ope_dir[j].power = obj->valueint;
//end for
//end for
cJSON_Delete(json);
return 0;
韦凯峰 Linux C/C++零基础编程教程
Linux系统编程,Openwrt系统开发
以上是关于json多级嵌套对象创建-第9讲的主要内容,如果未能解决你的问题,请参考以下文章