最近的项目中,涉及对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; //保存在一个二维数组中
	}
}
GitHub 加速计划 / cj / cJSON
10.45 K
3.16 K
下载
Ultralightweight JSON parser in ANSI C
最近提交(Master分支:2 个月前 )
424ce4ce This reverts commit 5b502cdbfb21fbe5f6cf9ffbd2b96e4281a741e6. Related to #860 4 个月前
32497300 - 5 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐