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

简介: 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: h

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;
}




目录
相关文章
|
3月前
|
存储 JSON NoSQL
cJSON项目解析
cJSON项目解析
|
11月前
|
JSON 编译器 API
[✔️]lua-cjson 编译,在unity中使用cjson
[✔️]lua-cjson 编译,在unity中使用cjson
150 0
|
11月前
Lua常用库
Lua常用库
70 0
|
存储 JSON API
cJSON使用教程
cJSON是一个使用C语言编写的JSON数据解析器,具有超轻便,可移植,单文件的特点,使用MIT开源协议。
cJSON使用教程
|
存储 JSON JavaScript
cJSON生成json字符串
嵌入式开发中如果需要上云常常需要处理json字符串,这时候可以调用json库如:cJSON,jasson等。本文介绍cJSON库使用
|
XML 存储 JSON
C/C++程序开发: cJSON的使用(创建与解析JSON数据)
C/C++程序开发: cJSON的使用(创建与解析JSON数据)
718 0
C/C++程序开发: cJSON的使用(创建与解析JSON数据)
|
机器学习/深度学习 移动开发 JSON
使用cJSON库解析JSON
cJSON库的下载 cJSON是一个基于C的JSON解析库,这个库非常简单,只有cJSON.c和cJSON.h两个文件,支持JSON的解析和封装,需要调用时,只需要#include "cJSON.h"就可以使用了, 库源码下载地址:cJSON download JSON官方网站:json 只包含键值对的JSON字符串解析 JSON字符串: { "name": "Andy", //键值对1 "age": 20 //键值对2 } 这个JSON对象只有两个键值对,键name对应字符串Andy,键age对应数字20。
2100 0