cJSON笔记——三种结构的cJSON数组

最近的项目中,涉及对cJSON库的使用,特别是不同结构的cJOSN数组的运用,在此小结以下。

1.指定(路径/文件类型/文件名)读取整个文本

/**
* @brief 
*
* @param file_dir  文件所在的路径
* @param file_name	文件名
* @param file_type	文件类型
*
* @brief 读取文件的内容,赋值给字符指针
*
* @return 成功:返回字符指针buf,失败:返回err
*/

char *read_json_file(char *file_dir,char *file_name,char *file_type)
{
	FILE *fp = NULL;
	char *out = NULL;
	char *buf = NULL;
	char *err = "error";	
	char *namep = NULL,*typep = NULL;

	DIR	*dp;
	struct dirent *filename;
	char file_path[200] = {0};

	dp = opendir(file_dir);
	if(!dp)
	{
		fprintf(stderr,"open directory error\n");
		return err;
	}
	
	while(filename == readdir(dp))
	{
		namep = strstr(filename->d_name,file_name);
		if(namep == NULL)
		{
			continue;
		}
	
		typep = strstr(filename->d_name,file_type);
		if(typep == NULL)
		{
			continue;
		}
		
		if(namep == filename->d_name)
		{

			//printf("filename:%-10s\t d_info:%ld\t\n",filename->d_name,filename->d_ino);

			strcpy(file_path,file_dir);
			strcat(file_path,"/");
			strcat(file_path,filename->d_name);
			//printf("file_path:%s\n",file_path);
            
			if(NULL != (fp = fopen(file_path,"rd")))
			{
				long len;
				int nread;

				fseek(fp,0,SEEK_END);
				len = ftell(fp);
				fseek(fp,0,SEEK_SET);
				if(len < 0)	
				{
					printf("invalid path\n");
				}	
				
				buf = (char *)malloc(len + 1);
				if(buf == NULL)
				{
					printf("NO enough memory!\n");
					exit(0);
				}

				nread = fread(buf,len,1,fp);
				if(!nread)
				{
					printf("Failed to read the config file\n");
				}
			
				fclose(fp);

				buf[len] = '\0';
							
			}	
		}
	}
	closedir(dp);
	return buf;
}

2.JSON

存在三种情况的json数组

1)没有key值的根数组

如:

{	
	[
		{
			"nodeId" : 1,
			...
		}
		,{
			"nodeId" : 2,
			...
		}
	]
}

生成json数组

	cJSON *jsonArray = cJSON_CreateArray(); 
	char *msg = NULL;
	cJSON *ArrayItem0 = cJSON_CreateObject();
//------0
	cJSON_AddStringToObject(ArrayItem0,"nodeId","1");
	cJSON_AddStringToObject(ArrayItem0,"key2","2");
	cJSON_AddItemToArray(jsonArray,ArrayItem0);
//------1
	cJSON_AddStringToObject(ArrayItem1,"nodeId","1");
	cJSON_AddStringToObject(ArrayItem1,"key2","2");

	cJSON_AddItemToArray(jsonArray,ArrayItem1);

	msg = cJSON_Print(jsonArray);

	printf("生成的JSON0:\n%s\n",msg);

解析json数组步骤:

//1.直接计算根数组的size	
array_size = cJSON_GetArraySize(root_json); 
//2.遍历数组成员
for(cnt = 0;cnt < array_size;cnt++)
{
	array_sub = cJSON_GetArrayItem(root_json,cnt);
	if(array_sub == NULL)
	{	
		continue;	
	}

	if((node_id_json= cJSON_GetObjectItem(array_sub,"nodeId")) == NULL)
	{
		WE_DBG("Could not find node id object\n");
		cJSON_Delete(root_json);
		return -1;
	}
	node_id = node_id_json->valueint;
}

2) 有key值的根数组

如:

{	
	key:[
			{
				"nodeId" : 1,
				...
			}
			,{
				"nodeId" : 2,
				...
			}
		]
}

生成json数组

	char *msg = NULL;	
    root_json=cJSON_CreateObject();   //创建根数据对象	
    cJSON_AddStringToObject(root_json,"key",cJSON *jsonArray = cJSON_CreateArray());	
	cJSON *ArrayItem0 = cJSON_CreateObject();
//------0
	cJSON_AddStringToObject(ArrayItem0,"nodeId","1");
	cJSON_AddStringToObject(ArrayItem0,"key2","2");
	cJSON_AddItemToArray(jsonArray,ArrayItem0);
//------1
	cJSON_AddStringToObject(ArrayItem1,"nodeId","1");
	cJSON_AddStringToObject(ArrayItem1,"key2","2");

	cJSON_AddItemToArray(jsonArray,ArrayItem1);

	msg = cJSON_Print(jsonArray);

	printf("生成的JSON0:\n%s\n",msg);

解析json数组步骤:

//1.先获取key对象
array_json = cJSON_GetObjectItem(root_json,"key");
//2.再计算根数组的size	
array_size = cJSON_GetArraySize(array_json); 
//3.遍历数组成员
for(cnt = 0;cnt < array_size;cnt++)
{
	array_sub = cJSON_GetArrayItem(array_json,cnt);
	if(array_sub == NULL)
	{	
		continue;	
	}

	if((node_id_json= cJSON_GetObjectItem(array_sub,"nodeId")) == NULL)
	{
		WE_DBG("Could not find node id object\n");
		cJSON_Delete(root_json);
		return -1;
	}
	node_id = node_id_json->valueint;
}

3)嵌套数组

如:

{	
	key:[
			{
				"nodeId" : 1,
				"port":[1,2,3],
				...
			}

		]
}

解析步骤:

//1.先获取key对象
array_json = cJSON_GetObjectItem(root_json,"key");
//2.再计算根数组的size	
array_size1 = cJSON_GetArraySize(array_json); 
//3.遍历数组成员
for(cnt = 0;cnt < array_size1;cnt++)
{
	array_sub1 = cJSON_GetArrayItem(array_json,cnt);
	if(array_sub1 == NULL)
	{	
		continue;	
	}

	if((node_id_json= cJSON_GetObjectItem(array_sub1,"nodeId")) == NULL)
	{
		WE_DBG("Could not find node id object\n");
		cJSON_Delete(root_json);
		return -1;
	}
	node_id = node_id_json->valueint;
	//4.解析内部数组
	array_json2 =cJSON_GetObjectItem(array_sub1,"port");//获取port对象
	array_size2 = cJSON_GetArraySize(array_json2);//计算内部数组的size

	for(cnt2 = 0;cnt2 < array_size2;cnt2++)
	{
		array_sub2 = cJSON_GetArrayItem(array_json2,cnt2); //遍历数组成员
		table[cnt1].port[cnt2] = array_sub2->valueint; //保存在一个二维数组中
	}
}
  • 8
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值