cJSON数组demo

文章展示了如何利用cJSON库在C语言中创建JSON对象和数组,包括添加数值和字符串属性,以及将JSON对象序列化成字符串。通过四个示例(demo_1至demo_4),逐步演示了从简单的JSON结构到嵌套数组的构建过程。
int cJSON_array_demo_1(void)
{
	cJSON *student = NULL;	
	
	cJSON *class = cJSON_CreateObject();
	if (NULL == class)
	{
		return -1;
	}
	
	cJSON_AddNumberToObject(class, "studentCnt", 2);
	
	cJSON *studentsArr = cJSON_CreateArray(); 
	
	student = cJSON_CreateObject();
	cJSON_AddNumberToObject(student, "number", 1);
	cJSON_AddStringToObject(student, "name", "xiaoming");
	cJSON_AddItemToArray(studentsArr, student);
	
	student = cJSON_CreateObject();
	cJSON_AddNumberToObject(student, "number", 2);
	cJSON_AddStringToObject(student, "name", "xiaohong");
	cJSON_AddItemToArray(studentsArr, student);
	
	cJSON_AddItemToObject(class, "students", studentsArr);
	char *strJson = cJSON_Print(class);
	printf("\n%s\n", strJson);
	
	cJSON_Delete(class);
	free(strJson);
}
/*
{
	"studentCnt":	2,
	"students":	[{
			"number":	1,
			"name":	"xiaoming"
		}, {
			"number":	2,
			"name":	"xiaohong"
		}]
}
*/
int cJSON_array_demo_2(void)
{
	cJSON *student = NULL;	
	cJSON *studentInfo = NULL;
	
	cJSON *class = cJSON_CreateObject();
	if (NULL == class)
	{
		return -1;
	}
	
	cJSON_AddNumberToObject(class, "studentCnt", 2);
	
	cJSON *studentsArr = cJSON_CreateArray(); 
	
	student = cJSON_CreateArray(); 
	studentInfo = cJSON_CreateObject();
	cJSON_AddNumberToObject(studentInfo, "number", 1);
	cJSON_AddStringToObject(studentInfo, "name", "xiaoming");
	cJSON_AddItemToArray(student, studentInfo);
	cJSON_AddItemToArray(studentsArr, student);
	
	student = cJSON_CreateArray(); 
	studentInfo = cJSON_CreateObject();
	cJSON_AddNumberToObject(studentInfo, "number", 2);
	cJSON_AddStringToObject(studentInfo, "name", "xiaohong");
	cJSON_AddItemToArray(student, studentInfo);
	cJSON_AddItemToArray(studentsArr, student);
	
	cJSON_AddItemToObject(class, "students", studentsArr);
	char *strJson = cJSON_Print(class);
	printf("\n%s\n", strJson);
	
	cJSON_Delete(class);
	free(strJson);
}
/*
{
	"studentCnt":	2,
	"students":	[[{
				"number":	1,
				"name":	"xiaoming"
			}], [{
				"number":	2,
				"name":	"xiaohong"
			}]]
}
*/
int cJSON_array_demo_3(void)
{
	cJSON *class = cJSON_CreateObject();
	if (NULL == class)
	{
		return -1;
	}
	
	cJSON *studentsArr = cJSON_CreateArray(); 
	cJSON_AddStringToObject(studentsArr, "name", "xiaoming");
	cJSON_AddStringToObject(studentsArr, "name", "xiaohong");
	
	cJSON_AddItemToObject(class, "students", studentsArr);
	char *strJson = cJSON_Print(class);
	printf("\n%s\n", strJson);
	
	cJSON_Delete(class);
	free(strJson);
}
/*
{
	"students":	["xiaoming", "xiaohong"]
}
*/
int cJSON_array_demo_4(void)
{
	const char name[][32] = {"xiaoming", "xiaohong"};
	const char *p[2];
	p[0] = name[0];
	p[1] = name[1];
	
	cJSON *class = cJSON_CreateObject();
	if (NULL == class)
	{
		return -1;
	}
	
	cJSON *studentsArr = cJSON_CreateStringArray(p, sizeof(name)/32);
	
	cJSON_AddItemToObject(class, "students", studentsArr);
	char *strJson = cJSON_Print(class);
	printf("\n%s\n", strJson);
	
	cJSON_Delete(class);
	free(strJson);
}
/*
{
	"students":	["xiaoming", "xiaohong"]
}
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值