使用cJSON库构造JSON
cJSON
Ultralightweight JSON parser in ANSI C
项目地址:https://gitcode.com/gh_mirrors/cj/cJSON
免费下载资源
·
一、构造简单的JSON字符串
1. 数字
cJSON库函数:
//在对象中添加数字
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number);
2. 字符串
cJSON库函数:
//在对象中添加字符串
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
3. 逻辑值
cJSON库函数:
//在对象中添加逻辑值
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name);//在对象中添加null
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name);//在对象中添加true
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name);//在对象中添加false
具体代码如下:
#include <stdio.h>
#include "cJSON.h"
int main(void)
{
char *name = "MQ";
int age = 25;
double height = 183.52;
cJSON *TCP = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(TCP,"name",name); //添加字符串
cJSON_AddNumberToObject(TCP,"age",age); //添加整型数字
cJSON_AddNumberToObject(TCP,"height",height); //添加浮点型数字
cJSON_AddFalseToObject(TCP,"gender"); //添加逻辑值false
cJSON_AddStringToObject(TCP,"name","MQ"); //添加字符串
cJSON_AddNumberToObject(TCP,"age",25); //添加整型数字
cJSON_AddNumberToObject(TCP,"height",183.52); //添加浮点型数字
cJSON_AddFalseToObject(TCP,"gender"); //添加逻辑值false
char *json_data = cJSON_Print(TCP); //JSON数据结构转换为JSON字符串
printf("%s\n",json_data); //输出字符串
cJSON_free(json_data); //释放空间
cJSON_Delete(TCP); //清除结构体
return 0;
}
运行结果如下:
{
"name": "MQ",
"age": 25,
"height": 183.52,
"gender": false,
"name": "MQ",
"age": 25,
"height": 183.52,
"gender": false
}
注意:定义浮点型(float)数据时,要使用双浮点型(double)定义。否则小数点后就会输出长串数字,导致失败。
二、构造嵌套数组的JSON字符串
cJSON库函数:
//创建整型数组
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);
//创建浮点型数组---这个使用会出问题,需要浮点型可以使用双浮点型函数。
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count);
//创建双浮点型数组
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);
具体代码如下:
#include <stdio.h>
#include "cJSON.h"
int main(void)
{
int time[4]={123,456,789,150};
float grade1[4]={66.51,118.52,61.53,128.54};
double grade2[4]={66.51,118.52,61.53,128.54};
cJSON *TCP = cJSON_CreateObject(); //创建一个对象
cJSON *TIM = cJSON_CreateIntArray(time,4); //创建一个整型数组
cJSON_AddItemToObject(TCP,"time",TIM);
cJSON *GRA1 = cJSON_CreateFloatArray(grade1,4); //创建一个浮点型数组
cJSON_AddItemToObject(TCP,"grade",GRA1);
cJSON *GRA2 = cJSON_CreateDoubleArray(grade2,4); //创建一个双浮点型数组
cJSON_AddItemToObject(TCP,"grade",GRA2);
char *json_data = cJSON_Print(TCP); //JSON数据结构转换为JSON字符串
printf("%s\n",json_data); //输出字符串
cJSON_free(json_data);
cJSON_Delete(TCP); //清除结构体
return 0;
}
运行结果如下:
{
"time": [123, 456, 789, 150],
"grade": [66.510002136230469, 118.51999664306641, 61.529998779296875, 128.53999328613281],
"grade": [66.51, 118.52, 61.53, 128.54]
}
注意:通过运行结果可以看出,使用浮点型(float)函数会出现小数点后输出长串数字的现象,导致失败
2. 字符型数组
cJSON库函数:
//创建字符型数组---这个使用会出问题,下面有替代方法。
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count);
具体替代方法代码如下:
#include <stdio.h>
#include "cJSON.h"
int main(void)
{
char subject[5][6]={"政治","数学","英语","专业课"};
cJSON *TCP = cJSON_CreateObject(); //创建一个对象
cJSON *SUB1 = cJSON_CreateArray(); //创建一个数组
cJSON_AddStringToObject(SUB1,"","政治"); //添加字符串到数组
cJSON_AddStringToObject(SUB1,"","数学"); //添加字符串到数组
cJSON_AddStringToObject(SUB1,"","英语"); //添加字符串到数组
cJSON_AddStringToObject(SUB1,"","专业课"); //添加字符串到数组
cJSON_AddItemToObject(TCP,"subject",SUB1); //添加数组到对象
cJSON *SUB2 = cJSON_CreateArray(); //创建一个数组
cJSON_AddStringToObject(SUB2,"",subject[0]); //添加字符串到数组
cJSON_AddStringToObject(SUB2,"",subject[1]); //添加字符串到数组
cJSON_AddStringToObject(SUB2,"",subject[2]); //添加字符串到数组
cJSON_AddStringToObject(SUB2,"",subject[3]); //添加字符串到数组
cJSON_AddItemToObject(TCP,"subject",SUB2); //添加字符串到数组
char *json_data = cJSON_Print(TCP); //JSON数据结构转换为JSON字符串
printf("%s\n",json_data);//输出字符串
cJSON_free(json_data);
cJSON_Delete(TCP);//清除结构体
return 0;
}
运行结果如下:
{
"subject": ["政治", "数学", "英语", "专业课"],
"subject": ["政治", "数学", "英语", "专业课"]
}
3. 对象型数组
cJSON库函数:
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void); //创建对象
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void); //创建数组
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item); //在数组中添加项目
具体代码如下:
#include <stdio.h>
#include "cJSON.h"
int main(void)
{
cJSON *TCP = cJSON_CreateObject(); //创建一个对象
cJSON *STU = cJSON_CreateArray(); //创建一个数组
cJSON *Z3 = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(Z3,"name","张三"); //添加字符串
cJSON_AddNumberToObject(Z3,"age",24); //添加整型数字
cJSON_AddTrueToObject(Z3,"gender"); //添加逻辑值
cJSON_AddItemToArray(STU,Z3); //添加对象到数组中
cJSON *L4 = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(L4,"name","李四"); //添加字符串
cJSON_AddNumberToObject(L4,"age",25); //添加整型数字
cJSON_AddTrueToObject(L4,"gender"); //添加逻辑值
cJSON_AddItemToArray(STU,L4); //添加对象到数组中
cJSON *W5 = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(W5,"name","王五"); //添加字符串
cJSON_AddNumberToObject(W5,"age",26); //添加整型数字
cJSON_AddTrueToObject(W5,"gender"); //添加逻辑值
cJSON_AddItemToArray(STU,W5); //添加对象到数组中
cJSON_AddItemToObject(TCP,"student",STU); //添加数组到对象中
char *json_data = cJSON_Print(TCP); //JSON数据结构转换为JSON字符串
printf("%s\n",json_data);//输出字符串
cJSON_free(json_data);
cJSON_Delete(TCP);//清除结构体
return 0;
}
运行结果如下:
{
"student":[
{"name":"张三","age":24,"gender":ture},
{"name":"李四","age":25,"gender":ture},
{"name":"王五","age":26,"gender":ture},
]
}
三、构造嵌套对象的JSON字符串
cJSON库函数:
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void); //创建对象
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number);//在对象中添加数字
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);//在对象中添加字符串
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item); //在对象中添加项目
具体代码如下:
#include <stdio.h>
#include "cJSON.h"
int main(void)
{
cJSON *TCP = cJSON_CreateObject(); //创建一个对象
cJSON *ADD = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(ADD,"country","China"); //添加字符串
cJSON_AddNumberToObject(ADD,"zip-code",123456); //添加整型数字
cJSON_AddItemToObject(TCP,"address",ADD);
cJSON *Z3 = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(Z3,"name","张三"); //添加字符串
cJSON_AddNumberToObject(Z3,"age",24); //添加整型数字
cJSON_AddTrueToObject(Z3,"gender"); //添加逻辑值
cJSON_AddItemToObject(TCP,"student1",Z3);
cJSON *L4 = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(L4,"name","李四"); //添加字符串
cJSON_AddNumberToObject(L4,"age",25); //添加整型数字
cJSON_AddTrueToObject(L4,"gender"); //添加逻辑值
cJSON_AddItemToObject(TCP,"student2",L4);
cJSON *W5 = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(W5,"name","王五"); //添加字符串
cJSON_AddNumberToObject(W5,"age",26); //添加整型数字
cJSON_AddTrueToObject(W5,"gender"); //添加逻辑值
cJSON_AddItemToObject(TCP,"student3",W5);
char *json_data = cJSON_Print(TCP); //JSON数据结构转换为JSON字符串
printf("%s\n",json_data);//输出字符串
cJSON_free(json_data);
cJSON_Delete(TCP);//清除结构体
return 0;
}
运行结果:
{
"address":{
"country": "China",
"zip-code": 123456
},
"student1":{
"name": "张三",
"age": 24,
"gender": true
},
"student2":{
"name": "李四",
"age": 25,
"gender": true
},
"student3":{
"name": "王五",
"age": 26,
"gender": true
}
}
四、构造无格式的JSON字符串
cJSON库函数:
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);//JSON数据结构转换为JSON字符串---有格式
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item); //JSON数据结构转换为JSON字符串---无格式
具体示例代码如下:
#include <stdio.h>
#include "cJSON.h"
int main(void)
{
double grade[4]={66.51,118.52,61.53,128.54};
int time[4]={123,456,789,150};
cJSON *TCP = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(TCP,"name","MQ"); //添加字符串
cJSON_AddNumberToObject(TCP,"age",25); //添加整型数字
cJSON_AddNumberToObject(TCP,"height",183.52); //添加浮点型数字
cJSON_AddFalseToObject(TCP,"gender"); //添加逻辑值false
cJSON *ADD = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(ADD,"country","China"); //添加字符串
cJSON_AddNumberToObject(ADD,"zip-code",123456); //添加整型数字
cJSON_AddItemToObject(TCP,"address",ADD);
cJSON *SUB = cJSON_CreateArray(); //创建一个数组
cJSON_AddStringToObject(SUB,"","政治"); //添加字符串到数组
cJSON_AddStringToObject(SUB,"","数学");
cJSON_AddStringToObject(SUB,"","英语");
cJSON_AddStringToObject(SUB,"","专业课");
cJSON_AddItemToObject(TCP,"subject",SUB); //添加数组到对象
cJSON *TIM = cJSON_CreateIntArray(time,4); //创建一个整型数组
cJSON_AddItemToObject(TCP,"time",TIM);
cJSON *GRA = cJSON_CreateDoubleArray(grade,4); //创建一个双浮点型数组
cJSON_AddItemToObject(TCP,"grade",GRA);
cJSON *STU = cJSON_CreateArray(); //创建一个数组
cJSON *Z3 = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(Z3,"name","张三"); //添加字符串
cJSON_AddNumberToObject(Z3,"age",24); //添加整型数字
cJSON_AddTrueToObject(Z3,"gender"); //添加逻辑值
cJSON_AddItemToArray(STU,Z3); //添加对象到数组中
cJSON *L4 = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(L4,"name","李四"); //添加字符串
cJSON_AddNumberToObject(L4,"age",25); //添加整型数字
cJSON_AddTrueToObject(L4,"gender"); //添加逻辑值
cJSON_AddItemToArray(STU,L4); //添加对象到数组中
cJSON *W5 = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(W5,"name","王五"); //添加字符串
cJSON_AddNumberToObject(W5,"age",26); //添加整型数字
cJSON_AddTrueToObject(W5,"gender"); //添加逻辑值
cJSON_AddItemToArray(STU,W5); //添加对象到数组中
cJSON_AddItemToObject(TCP,"student",STU); //添加数组到对象中
char *json_data = cJSON_PrintUnformatted(TCP); //JSON数据结构转换为JSON字符串
printf("%s\n",json_data);//输出字符串
cJSON_free(json_data);
cJSON_Delete(TCP);//清除结构体
return 0;
}
运行结果:
{"name":"MQ","age":25,"height":183.52,"gender":false,"address":{"country":"China","zip-code":123456},"subject":["政治","数学","英语","专业课"],"time":[123,456,789,150],"grade":[66.51,118.52,61.53,128.54],"student":[{"name":"张三","age":24,"gender":true},{"name":"李四","age":25,"gender":true},{"name":"王五","age":26,"gender":true}]}
五、混合构造
#include <stdio.h>
#include "cJSON.h"
int main(void)
{
double grade[4]={66.51,118.52,61.53,128.54};
int time[4]={123,456,789,150};
cJSON *TCP = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(TCP,"name","MQ"); //添加字符串
cJSON_AddNumberToObject(TCP,"age",25); //添加整型数字
cJSON_AddNumberToObject(TCP,"height",183.52); //添加浮点型数字
cJSON_AddFalseToObject(TCP,"gender"); //添加逻辑值false
cJSON *ADD = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(ADD,"country","China"); //添加字符串
cJSON_AddNumberToObject(ADD,"zip-code",123456); //添加整型数字
cJSON_AddItemToObject(TCP,"address",ADD);
cJSON *SUB = cJSON_CreateArray(); //创建一个数组
cJSON_AddStringToObject(SUB,"","政治"); //添加字符串到数组
cJSON_AddStringToObject(SUB,"","数学");
cJSON_AddStringToObject(SUB,"","英语");
cJSON_AddStringToObject(SUB,"","专业课");
cJSON_AddItemToObject(TCP,"subject",SUB); //添加数组到对象
cJSON *TIM = cJSON_CreateIntArray(time,4); //创建一个整型数组
cJSON_AddItemToObject(TCP,"time",TIM);
cJSON *GRA = cJSON_CreateDoubleArray(grade,4); //创建一个双浮点型数组
cJSON_AddItemToObject(TCP,"grade",GRA);
cJSON *STU = cJSON_CreateArray(); //创建一个数组
cJSON *Z3 = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(Z3,"name","张三"); //添加字符串
cJSON_AddNumberToObject(Z3,"age",24); //添加整型数字
cJSON_AddTrueToObject(Z3,"gender"); //添加逻辑值
cJSON_AddItemToArray(STU,Z3); //添加对象到数组中
cJSON *L4 = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(L4,"name","李四"); //添加字符串
cJSON_AddNumberToObject(L4,"age",25); //添加整型数字
cJSON_AddTrueToObject(L4,"gender"); //添加逻辑值
cJSON_AddItemToArray(STU,L4); //添加对象到数组中
cJSON *W5 = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(W5,"name","王五"); //添加字符串
cJSON_AddNumberToObject(W5,"age",26); //添加整型数字
cJSON_AddTrueToObject(W5,"gender"); //添加逻辑值
cJSON_AddItemToArray(STU,W5); //添加对象到数组中
cJSON_AddItemToObject(TCP,"student",STU); //添加数组到对象中
char *json_data = cJSON_Print(TCP); //JSON数据结构转换为JSON字符串
printf("%s\n",json_data);//输出字符串
cJSON_free(json_data);
cJSON_Delete(TCP);//清除结构体
return 0;
}
具体示例代码如下:
#include <stdio.h>
#include "cJSON.h"
int main(void)
{
double grade[4]={66.51,118.52,61.53,128.54};
int time[4]={123,456,789,150};
cJSON *TCP = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(TCP,"name","MQ"); //添加字符串
cJSON_AddNumberToObject(TCP,"age",25); //添加整型数字
cJSON_AddNumberToObject(TCP,"height",183.52); //添加浮点型数字
cJSON_AddFalseToObject(TCP,"gender"); //添加逻辑值false
cJSON *ADD = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(ADD,"country","China"); //添加字符串
cJSON_AddNumberToObject(ADD,"zip-code",123456); //添加整型数字
cJSON_AddItemToObject(TCP,"address",ADD);
cJSON *SUB = cJSON_CreateArray(); //创建一个数组
cJSON_AddStringToObject(SUB,"","政治"); //添加字符串到数组
cJSON_AddStringToObject(SUB,"","数学");
cJSON_AddStringToObject(SUB,"","英语");
cJSON_AddStringToObject(SUB,"","专业课");
cJSON_AddItemToObject(TCP,"subject",SUB); //添加数组到对象
cJSON *TIM = cJSON_CreateIntArray(time,4); //创建一个整型数组
cJSON_AddItemToObject(TCP,"time",TIM);
cJSON *GRA = cJSON_CreateDoubleArray(grade,4); //创建一个双浮点型数组
cJSON_AddItemToObject(TCP,"grade",GRA);
cJSON *STU = cJSON_CreateArray(); //创建一个数组
cJSON *Z3 = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(Z3,"name","张三"); //添加字符串
cJSON_AddNumberToObject(Z3,"age",24); //添加整型数字
cJSON_AddTrueToObject(Z3,"gender"); //添加逻辑值
cJSON_AddItemToArray(STU,Z3); //添加对象到数组中
cJSON *L4 = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(L4,"name","李四"); //添加字符串
cJSON_AddNumberToObject(L4,"age",25); //添加整型数字
cJSON_AddTrueToObject(L4,"gender"); //添加逻辑值
cJSON_AddItemToArray(STU,L4); //添加对象到数组中
cJSON *W5 = cJSON_CreateObject(); //创建一个对象
cJSON_AddStringToObject(W5,"name","王五"); //添加字符串
cJSON_AddNumberToObject(W5,"age",26); //添加整型数字
cJSON_AddTrueToObject(W5,"gender"); //添加逻辑值
cJSON_AddItemToArray(STU,W5); //添加对象到数组中
cJSON_AddItemToObject(TCP,"student",STU); //添加数组到对象中
char *json_data = cJSON_Print(TCP); //JSON数据结构转换为JSON字符串
printf("%s\n",json_data);//输出字符串
cJSON_free(json_data);
cJSON_Delete(TCP);//清除结构体
return 0;
}
GitHub 加速计划 / cj / cJSON
10.45 K
3.16 K
下载
Ultralightweight JSON parser in ANSI C
最近提交(Master分支:2 个月前 )
424ce4ce
This reverts commit 5b502cdbfb21fbe5f6cf9ffbd2b96e4281a741e6.
Related to #860
4 个月前
32497300 - 5 个月前
更多推荐
已为社区贡献3条内容
所有评论(0)