cJSON数组demo

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"]
}
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Cjson 是一个用于处理 JSON 数据的库,它提供了一些函数来进行 JSON 数据的解析和生成。在 Cjson 中,可以通过函数 cJSON_Parse() 将一个 JSON 字符串解析为一个 cJSON 对象,而 cJSON_Print() 函数可以将一个 cJSON 对象转换为一个 JSON 字符串。 以下是一个示例代码,展示了如何在 Cjson 中进行数组和字符串的互转: ```c #include <stdio.h> #include <stdlib.h> #include "cJSON.h" int main() { // 创建一个 JSON 数组 cJSON *array = cJSON_CreateArray(); // 向数组中添加字符串元素 cJSON_AddItemToArray(array, cJSON_CreateString("element1")); cJSON_AddItemToArray(array, cJSON_CreateString("element2")); cJSON_AddItemToArray(array, cJSON_CreateString("element3")); // 将数组转换为 JSON 字符串 char *jsonStr = cJSON_Print(array); printf("JSON String: %s\n", jsonStr); // 解析 JSON 字符串为 cJSON 数组 cJSON *parsedArray = cJSON_Parse(jsonStr); // 遍历数组并打印每个元素 int arraySize = cJSON_GetArraySize(parsedArray); for (int i = 0; i < arraySize; i++) { cJSON *element = cJSON_GetArrayItem(parsedArray, i); printf("Element %d: %s\n", i, element->valuestring); } // 释放内存 free(jsonStr); cJSON_Delete(array); cJSON_Delete(parsedArray); return 0; } ``` 以上代码中,我们首先创建了一个 cJSON 数组,并向数组中添加了三个字符串元素。然后,我们使用 cJSON_Print() 函数将该数组转换为 JSON 字符串,并打印出来。接着,我们使用 cJSON_Parse() 函数将该 JSON 字符串解析为一个 cJSON 数组。最后,我们遍历该数组并打印出每个元素的值。 请注意,在使用 cJSON 库之前,你需要先下载并编译该库。你可以从 cJSON 的官方网站(https://github.com/DaveGamble/cJSON)下载该库的源代码,并根据其提供的文档进行编译和安装。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值