cJSON学习练练手--解析与组装JSON数据

前端之家收集整理的这篇文章主要介绍了cJSON学习练练手--解析与组装JSON数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最近换了工作,进了研华,从事物联网相关的工作。

学习了几篇JSON的文章,顺便下载了一个cJSON的源码研究了下,轻松掌握了JSON相关的东西,挺简单的,后面物联网相关的知识会接触到,先预热一波,提高工作效率。下面写了测试一个例子,例子的功能主要如下:

自己手写一个json文件:node.json,先使用cJSON解析node.json然后打印,接下来自己组装一个自定义的json结构,然后打印并写入json_test.json。

node.json

{
	"People":[
		{"name":"yangyuanxin","telphone":"12345","age":88,"email":"mssuan@vip.qq.com","value":true},{"name":"study","telphone":"66666","email":"mmmm@vip.qq.com"}
	]

}
C代码
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "cJSON.h"
#define NR(x) (sizeof(x)/sizeof(x[0]))
#define json_file_name "C:\\Users\\Administrator\\Desktop\\解析json数据学习\\json_test_study\\node.json"

int file_size(char* filename)  
{  
    FILE *fp=fopen(filename,"r");  
    if(!fp) return -1;  
    fseek(fp,0L,SEEK_END);  
    int size=ftell(fp);  
    fclose(fp);        
    return size;  
}  

//json的自定义格式 
struct People_record 
{
	const char *name ;
	const char *telphone ;
	int age ;
	const char *email ; 
	int value ;
};

struct People_record record[2]={
		{"yangyuanxin","12345",88,"mssuan@vip.qq.com",cJSON_True},{"study","66666","mmmm@vip.qq.com",cJSON_False},};


int main(void)
{
	int i ;
	int fd = -1 ;
	int fd1 = -1 ;
	int ret = -1 ;
	int json_file_len = 0 ;
	char *json_test_buffer = NULL ;
	char *out ; 
	cJSON *json ;
	cJSON *root,*fmt,*fld;
	fd = open(json_file_name,O_RDWR);
	if(-1 == fd){
		printf("open json_file fair!\n");
		return -1 ;
	}
	//获取文件的长度 
	json_file_len = file_size(json_file_name);
	if(0 == json_file_len){
		printf("json_file_len is NULL!\n");
		return -2 ;
	}
	//给缓存分配空间用于存储文件中的json数据 
	json_test_buffer = malloc(json_file_len);
	if(NULL == json_test_buffer){
		printf("malloc json_test_buffer fair!\n");
		return -3 ;
	}
	ret = read(fd,json_test_buffer,json_file_len);
	if(-1 == ret){
		printf("read data error!\n");
		return -4 ;
	}
	close(fd); //关闭文件句柄 
	printf("打印文件中的json数据\n");
	//解析数据 
	json=cJSON_Parse(json_test_buffer);
	if (!json) 
	{
		printf("Error before: [%s]\n",cJSON_GetErrorPtr());
		return -1 ;
	}
	else
	{
		//打印数据 
		out = cJSON_Print(json);
		cJSON_Delete(json);
		printf("%s\n",out);
		free(out);
		out = NULL ;
	}
	printf("组装一个json数据\n");
	
	//组装数据 
	root=cJSON_CreateObject();	//创建根 object
	cJSON_AddItemToObject(root,"People",fld=cJSON_CreateArray()); //往根object中添加一个json数组
	//组装json 
	for(i = 0 ; i < NR(record) ; i++){
		//往array中添加object 
		cJSON_AddItemToArray(fld,fmt=cJSON_CreateObject());  
		cJSON_AddStringToObject(fmt,"name",record[i].name);
		cJSON_AddStringToObject(fmt,"telephone",record[i].telphone);
		cJSON_AddNumberToObject(fmt,"age",record[i].age);
		cJSON_AddStringToObject(fmt,"email",record[i].email);
		cJSON_AddBoolToObject(fmt,"value",record[i].value);
	}
	out=cJSON_Print(root);	
	fd1 = open("json_test.json",O_RDWR|O_TRUNC);
	if(fd1 < 0){
		fd1 = open("json_test.json",O_CREAT|O_RDWR|O_TRUNC);
		if(fd1 < 0){
			printf("create file fair!\n")	;
			close(fd1);
			return -1 ;
		}
	}
	printf("open file success!\n");
	ret = write(fd1,out,strlen(out));
	if(-1 == ret){
		printf("write file fair!\n");
		return -2 ;
	}
	close(fd1);
	cJSON_Delete(root);	
	printf("%s\n",out);	
	free(out);
	free(json_test_buffer);
	out = NULL ;
	json_test_buffer = NULL ;
	return 0 ;
}

运行结果:


json_test.json

{
	"People":	[{
			"name":	"yangyuanxin","telephone":	"12345","age":	88,"email":	"mssuan@vip.qq.com","value":	true
		},{
			"name":	"study","telephone":	"66666","age":	0,"email":	"mmmm@vip.qq.com","value":	false
		}]
}
原文链接:https://www.f2er.com/json/288627.html

猜你在找的Json相关文章