C language: use cJSON library to parse JSON strings

C language: cJSON library usage detailed explanation
C language: use cJSON library to construct JSON
C language: use cJSON library to parse JSON strings


1. Introduction to cJSON library functions

Introduce some functions that are often used when parsing JSON. Use the following functions to complete most of the JSON format parsing.
The specific code is as follows:

cJSON *cJSON_Parse(const char *value);
/*作用:将一个JSON数据包,按照cJSON结构体的结构序列化整个数据包,并在堆中开辟一块内存存储cJSON结构体
返回值:成功返回一个指向内存块中的cJSON的指针,失败返回NULL*/

cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
/*作用:获取JSON字符串字段值
返回值:成功返回一个指向cJSON类型的结构体指针,失败返回NULL*/

int cJSON_GetArraySize(cJSON *array);
/*作用:获取数组成员对象个数
返回值:数组成员对象个数*/

void  cJSON_Delete(cJSON *c);
/*作用:释放位于堆中cJSON结构体内存
返回值:无*/

Second, use the cJSON library to parse JSON strings

The purpose of using cJSON to parse the JSON string is to extract the data we want to obtain, and then analyze and process it.
There are generally three situations when using cJSON to parse JSON strings.

1. Parse a simple JSON string

Parsing simple JSON strings is mainly to obtain strings, numbers and logical values.
The specific code is as follows:

#include <stdio.h>
#include "cJSON.h"
 
int main()
{
    
    
	char json_string[]="{\"name\":\"MQ\",\"age\":25,\"height\":183.5,\"gender\":true}";//定义JSON字符串	
 
	cJSON* cjson = cJSON_Parse(json_string);//将JSON字符串转换成JSON结构体
	if(cjson == NULL)						//判断转换是否成功
	{
    
    
		printf("cjson error...\r\n");
	}
	else
	{
    
    
		printf("%s\n",cJSON_Print(cjson));//打包成功调用cJSON_Print打印输出
	}
	
	printf("/*********************以下就是提取的数据**********************/\n");
	char *name = cJSON_GetObjectItem(cjson,"name")->valuestring;	//解析字符串
	printf("%s\n",name);
	int age = cJSON_GetObjectItem(cjson,"age")->valueint;			//解析整型
	printf("%d\n",age);
	double height = cJSON_GetObjectItem(cjson,"height")->valuedouble;	//解析双浮点型
	printf("%.1f\n",height);
	int gender = cJSON_GetObjectItem(cjson,"gender")->type; 	//解析逻辑值---输出逻辑值对应的宏定义数值
	printf("%d\n",gender);

	cJSON_Delete(cjson);//清除结构体 
	return 0;
}

The results are as follows:

{
    
    
	"name": "MQ",		//字符串
	"age":	25,			//整数
	"height": 183.5,	//浮点数
	"gender": true,		//逻辑值
}
/*********************以下就是提取的数据**********************/
MQ
25
183.5
2

2. Parse the JSON string of the nested array

Parsing the JSON string of a nested array is mainly to get the data in the array.
The specific code is as follows:

#include <stdio.h>
#include "cJSON.h"
 
int main()
{
    
    
	char json_string[]="{\"subject\":[\"政治\",\"数学\",\"英语\",\"专业课\"],\
	                       \"time\":[123,456,789,150],\"grade\":[66.51,118.52,61.53,128.54],\
			 				 \"student\":[{\"name\":\"张三\",\"age\":24,\"gender\":false},\
	 			  						  {\"name\":\"李四\",\"age\":25,\"gender\":true},\
	                     				  {\"name\":\"王五\",\"age\":26,\"gender\":null}]}";//定义JSON字符串	
 
	cJSON* cjson = cJSON_Parse(json_string);//将JSON字符串转换成JSON结构体
	if(cjson == NULL)						//判断转换是否成功
	{
    
    
		printf("cjson error...\r\n");
	}
	else
	{
    
    
		printf("%s\n",cJSON_Print(cjson));//打包成功调用cJSON_Print打印输出
	}
	
	printf("/*********************以下就是提取的数据**********************/\n");
	cJSON* SUB = cJSON_GetObjectItem(cjson,"subject");	//解析数组
	int SUB_size = cJSON_GetArraySize(SUB);	//获取数组成员个数 
	int i=0;
	for(i=0;i<SUB_size;i++)
	{
    
    
		printf("%s ",cJSON_GetArrayItem(SUB,i)->valuestring);//解析数组中的字符串
	}
	printf("\n");
	cJSON* TIM = cJSON_GetObjectItem(cjson,"time");	//解析数组
	int TIM_size = cJSON_GetArraySize(TIM);//获取数组成员个数 
	for(i=0;i<TIM_size;i++)
	{
    
    
		printf("%d ",cJSON_GetArrayItem(TIM,i)->valueint);//解析数组中的整型数字
	}
	printf("\n");	
	cJSON* GRA = cJSON_GetObjectItem(cjson,"grade");//解析数组
	int GRA_size = cJSON_GetArraySize(GRA);	//获取数组成员个数 
	for(i=0;i<GRA_size;i++)
	{
    
    
		printf("%f ",cJSON_GetArrayItem(GRA,i)->valuedouble);//解析数组中的浮点型数字
	}	
	printf("\n");	
	cJSON* STU = cJSON_GetObjectItem(cjson,"student");//解析数组
	int STU_size = cJSON_GetArraySize(STU);//获取数组成员个数
	cJSON* STU_item = STU->child;//获取子对象
	for(i=0;i<STU_size;i++) 
	{
    
    
		printf("%s ",cJSON_GetObjectItem(STU_item,"name")->valuestring);//解析数组中对象中的字符串
		printf("%d ",cJSON_GetObjectItem(STU_item,"age")->valueint);//解析数组中对象中的整型数字
		printf("%d\n",cJSON_GetObjectItem(STU_item,"gender")->type);//解析数组中对象中的逻辑值---输出逻辑值对应的宏定义数值
		STU_item = STU_item->next;	//跳转到下一个对象中
	}
	cJSON_Delete(cjson);//清除结构体 
	return 0;
}

The results are as follows:

{
    
    
	"subject":      ["政治", "数学", "英语", "专业课"],	//字符型数组
	"time":        	[123, 456, 789, 150],		  		//整型数组
    "grade":        [66.51, 118.52, 61.53, 128.54],		//浮点型数组
    
    
	"student":[
				{
    
    "name":"张三","age":24,"gender":false},
				{
    
    "name":"李四","age":25,"gender":true},
				{
    
    "name":"王五","age":26,"gender":null}
			  ]										//对象型数组
			  
}

/*********************以下就是提取的数据**********************/
政治 数学 英语 专业课
123 456 789 150
66.510000 118.520000 61.530000 128.540000
张三 24 1
李四 25 2
王五 26 4

3. Parse the JSON format of nested objects

Parsing the JSON format of a nested object is mainly to obtain the data in the object.
The specific code is as follows:

#include <stdio.h>
#include "cJSON.h"
 
int main()
{
    
    
	char json_string[]="{\"address\":{\"country\":\"China\",\"zip-code\":123456}}";//定义JSON字符串	

	cJSON* cjson = cJSON_Parse(json_string);//将JSON字符串转换成JSON结构体
	if(cjson == NULL)						//判断转换是否成功
	{
    
    
		printf("cjson error...\r\n");
	}
	else
	{
    
    
		printf("%s\n",cJSON_Print(cjson));//打包成功调用cJSON_Print打印输出
	}
	
	printf("/*********************以下就是提取的数据**********************/\n");
	cJSON* ADD = cJSON_GetObjectItem(cjson,"address");	//解析对象
	char * country = cJSON_GetObjectItem(ADD,"country")->valuestring;	//解析对象中的字符串
	printf("%s\n",country);
	int zip = cJSON_GetObjectItem(ADD,"zip-code")->valueint;	//解析对象中的整型数字
	printf("%d\n",zip);
	
	cJSON_Delete(cjson);//清除结构体 
	return 0;
}

The results are as follows:

{
    
    
	"address":{
    
    		"country": "China",
        			"zip-code": 123456
        	  },		//对象			  
}
/*********************以下就是提取的数据**********************/

China
123456

Guess you like

Origin blog.csdn.net/MQ0522/article/details/112857786