关闭

cJSON系列(1) - cJSON 入门与应用

标签: cjsonc语言
2075人阅读 评论(0) 收藏 举报
分类:

cJSON 入门与应用

1. cJSON简介

cJSON aims to be the dumbest possible parser that you can get your job done with. It's a single file of C, and a single header file.

JSON is described best here: http://www.json.org/ It's like XML, but fat-free. You use it to move data around, store things, or just generally represent your program's state.

cJSON 下载地址:https://github.com/DaveGamble/cJSON

2. cJSON常用函数简介

cJSON * cJSON_CreateObject();
创建一个json对象,返回一个cJSON结构体类型的指针。

cJSON *cJSON_CreateArray();
创建一个数组对象,返回一个cJSON结构体类型的指针。

cJSON *cJSON_CreateString(const char *string);
创建一个字符串对象,传入一个char *类型的字符串,返回一个cJSON结构体类型的指针。

void cJSON_AddItemToArray(cJSON *array, cJSON *item);
向数组对象中添加一个元素,传入参数array为cJSON *结构体类型的指针,为数组对象; item为添加入数字对象中的对象指针。

void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
向json对象中添加一对元素,object为json对象,string为加入一对元素中的name,item为加入一对元素中的value。

cJSON *cJSON_Parse(const char *value);
解析一个json串,传入一个json格式的字符串,返回一个cJSON *类型的结构体指针。

char  *cJSON_Print(cJSON *item);
将一个cJSON结构体代表的json对象转换为一个json格式的字符串。

void   cJSON_Delete(cJSON *c);
释放一个cJSON对象所占用的内存空间。

3. 应用

以此json串为例进行json的生成和解析:
{
	"name": "中国",
	"province": [{
		"name": "黑龙江",
		"cities": {
			"city": ["哈尔滨", "大庆"]
		}
	}, {
		"name": "广东",
		"cities": {
			"city": ["广州", "深圳", "珠海"]
		}
	}, {
		"name": "台湾",
		"cities": {
			"city": ["台北", "高雄"]
		}
	}, {
		"name": "新疆",
		"cities": {
		        "city": ["乌鲁木齐"]
		}
	}]
}

3.1 生成json

#include <stdio.h>

#include "cJSON.h"
#include "cJSON_Utils.h"

#pragma comment(lib,"cjson.lib")

int main(void)
{
	cJSON * json = cJSON_CreateObject();

	cJSON *provinceArray = cJSON_CreateArray();

	cJSON *heilongjiang = cJSON_CreateObject();
	cJSON *hljcities = cJSON_CreateObject();
	cJSON *hljcityArray = cJSON_CreateArray();

	cJSON *guangdong = cJSON_CreateObject();
	cJSON *gdcities = cJSON_CreateObject();
	cJSON *gdcityArray = cJSON_CreateArray();

	cJSON *taiwan = cJSON_CreateObject();
	cJSON *twcities = cJSON_CreateObject();
	cJSON *twcityArray = cJSON_CreateArray();

	cJSON *xinjiang = cJSON_CreateObject();
	cJSON *xjcities = cJSON_CreateObject();
	cJSON *xjcityArray = cJSON_CreateArray();
	
	cJSON_AddStringToObject(json, "name", "中国");

	cJSON_AddStringToObject(heilongjiang, "name", "黑龙江");
	cJSON_AddItemToArray(hljcityArray, cJSON_CreateString("哈尔滨"));
	cJSON_AddItemToArray(hljcityArray, cJSON_CreateString("大庆"));
	cJSON_AddItemToObject(hljcities, "city", hljcityArray);
	cJSON_AddItemToObject(heilongjiang, "cities", hljcities);

	cJSON_AddStringToObject(guangdong, "name", "广东");
	cJSON_AddItemToArray(gdcityArray, cJSON_CreateString("广州"));
	cJSON_AddItemToArray(gdcityArray, cJSON_CreateString("深圳"));
	cJSON_AddItemToArray(gdcityArray, cJSON_CreateString("珠海"));
	cJSON_AddItemToObject(gdcities, "city", gdcityArray);
	cJSON_AddItemToObject(guangdong, "cities", gdcities);

	cJSON_AddStringToObject(taiwan, "name", "台湾");
	cJSON_AddItemToArray(twcityArray, cJSON_CreateString("台北"));
	cJSON_AddItemToArray(twcityArray, cJSON_CreateString("高雄"));
	cJSON_AddItemToObject(twcities, "city", twcityArray);
	cJSON_AddItemToObject(taiwan, "cities", twcities);

	cJSON_AddStringToObject(xinjiang, "name", "新疆");
	cJSON_AddItemToArray(xjcityArray, cJSON_CreateString("乌鲁木齐"));
	cJSON_AddItemToObject(xjcities, "city", xjcityArray);
	cJSON_AddItemToObject(xinjiang, "cities", xjcities);
	
	cJSON_AddItemToArray(provinceArray, heilongjiang);
	cJSON_AddItemToArray(provinceArray, guangdong);
	cJSON_AddItemToArray(provinceArray, taiwan);
	cJSON_AddItemToArray(provinceArray, xinjiang);

	cJSON_AddItemToObject(json, "province", provinceArray);


	printf("%s\n", cJSON_Print(json));


	if ( NULL != json )
	{
		cJSON_Delete(json);
		json = NULL;
	}
	
	return 0;
}

3.2 解析json

#include <stdio.h>

#include "cJSON.h"
#include "cJSON_Utils.h"


#pragma comment(lib,"cjson.lib")

int main(void)
{
 	const char *jsonStr = "{						\
 		\"name\": \"中国\",						\
 		\"province\": [{						\
 			\"name\": \"黑龙江\",					\
 			\"cities\": {						\
 				\"city\": [\"哈尔滨\", \"大庆\"]		\
 			}							\
 		}, {								\
 			\"name\": \"广东\",					\
 			\"cities\": {						\
 				\"city\": [\"广州\", \"深圳\", \"珠海\"]	\
 			}							\
 		}, {								\
 			\"name\": \"台湾\",					\
 			\"cities\": {						\
 				\"city\": [\"台北\", \"高雄\"]			\
 			}							\
 		}, {								\
 			\"name\": \"新疆\",					\
 			\"cities\": {						\
 			\"city\": [\"乌鲁木齐\"]				\
 			}							\
 		}]								\
 	}";

	cJSON *json = cJSON_Parse(jsonStr);

	if ( NULL != json )
	{
		cJSON * temp = cJSON_GetObjectItem(json, "name");

		if ( NULL != temp )
			printf( "name : %s\n", temp->valuestring);

		temp = cJSON_GetObjectItem(json, "province");

		printf( "province : \n");
		if ( NULL != temp )
		{
			int i = 0;
			int icount = cJSON_GetArraySize(temp);
			for (; i < icount; ++i)
			{
				cJSON * province = cJSON_GetArrayItem(temp, i);

				if ( NULL != province)
				{
					cJSON * name = NULL;
					cJSON * cities = NULL;
					
					name = cJSON_GetObjectItem(province, "name");
					cities = cJSON_GetObjectItem(province, "cities");
					if ( NULL != name )
						printf("    name : %s\n", name->valuestring);

					printf("    cities : \n");
					if ( NULL != cities )
					{
						cJSON * city = cJSON_GetObjectItem(cities, "city");
						printf ("        city:");
						if ( NULL != city )
						{
							int j = 0;
							int jcount = cJSON_GetArraySize(city);
							for (; j < jcount; ++j)
							{
								cJSON *cityItem = cJSON_GetArrayItem(city, j);
								if ( NULL != cityItem )
									printf ("%s ", cityItem->valuestring);
							}
						}
						printf ("\n\n");
					}
				}
			}
		}

		cJSON_Delete(json);
		json = NULL;
	}

	return 0;
}




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场