cJSON数组demo
cJSON
Ultralightweight JSON parser in ANSI C
项目地址:https://gitcode.com/gh_mirrors/cj/cJSON

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




Ultralightweight JSON parser in ANSI C
最近提交(Master分支:19 天前 )
c859b25d
18 天前
74e1ff49
this fixes CVE-2025-57052 23 天前
更多推荐
所有评论(0)