服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C/C++中CJSON的使用(创建与解析JSON数据)

C/C++中CJSON的使用(创建与解析JSON数据)

2022-01-10 15:08DS小龙哥 C/C++

cJSON是一个超轻巧的JSON解析器,本文主要介绍了C/C++中CJSON的使用(创建与解析JSON数据),具有一定的参考价值,感兴趣的可以了解一下

一、cjson介绍

cjson 是一个超轻巧,携带方便,单文件,可以作为 ansi-c 标准的 json 解析器,是一个用c语言编写的简单好用的json解析器;它只包含一个c文件和一个头文件,可以非常容易集成到自己工程项目中。

并且cjson是用ansi c(c89)编写的,可以兼容所有支持c语言的平台和编译器。

cjson下载地址:https://sourceforge.net/projects/cjson/

cjson的github仓库地址:https://github.com/davegamble/cjson

C/C++中CJSON的使用(创建与解析JSON数据)

二、json简介、语法介绍

2.1 json是什么?

json是javascript object notation(javascript对象表示法),是一种轻量级的数据交换格式。

json主要是用来存储和交换文本信息,类似xml格式;但是json比xml更小、更快,更易解析。

json是基于ecmascript (欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。

简洁和清晰的层次结构使得 json 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。 比如: web服务器接口基本都是采用json反馈数据,采用json格式字符串来描述符信息。 json文件的后缀一般是.json,这个只是为了方便辨识。

简单的说,json就是按照指定格式序列化的字符串,就算不使用任何现成的解析库,自己也可以按照正常解析字符串的思路去解析;有现成的标准json解析库,那就大大减轻了我们的工作量。

json格式的数据示例: 这是表示当前时间的json字符串

?
1
2
3
4
5
6
7
8
9
10
11
12
{
  "success": "1",
  "result": {
    "timestamp": "1631849514",
    "datetime_1": "2021-09-17 20:31:54",
    "datetime_2": "2021年09月17日 20时31分54秒",
    "week_1": "5",
    "week_2": "星期五",
    "week_3": "周五",
    "week_4": "friday"
  }
}

json格式的数据示例: 这是表示未来几天天气预报的json字符串

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
{
  "success": "1",
  "result": [
    {
      "weaid": "1",
      "days": "2021-09-17",
      "week": "星期五",
      "cityno": "beijing",
      "citynm": "北京",
      "cityid": "101010100",
      "temperature": "26℃/17℃",
      "humidity": "0%/0%",
      "weather": "晴",
      "weather_icon": "http://api.k780.com/upload/weather/d/0.gif",
      "weather_icon1": "http://api.k780.com/upload/weather/n/0.gif",
      "wind": "北风转西南风",
      "winp": "小于3级",
      "temp_high": "26",
      "temp_low": "17",
      "humi_high": "0",
      "humi_low": "0",
      "weatid": "1",
      "weatid1": "1",
      "windid": "8",
      "winpid": "0",
      "weather_iconid": "0",
      "weather_iconid1": "0"
    },
    {
      "weaid": "1",
      "days": "2021-09-18",
      "week": "星期六",
      "cityno": "beijing",
      "citynm": "北京",
      "cityid": "101010100",
      "temperature": "25℃/17℃",
      "humidity": "0%/0%",
      "weather": "多云",
      "weather_icon": "http://api.k780.com/upload/weather/d/1.gif",
      "weather_icon1": "http://api.k780.com/upload/weather/n/1.gif",
      "wind": "西南风",
      "winp": "小于3级",
      "temp_high": "25",
      "temp_low": "17",
      "humi_high": "0",
      "humi_low": "0",
      "weatid": "2",
      "weatid1": "2",
      "windid": "5",
      "winpid": "0",
      "weather_iconid": "1",
      "weather_iconid1": "1"
    },
    {
      "weaid": "1",
      "days": "2021-09-19",
      "week": "星期日",
      "cityno": "beijing",
      "citynm": "北京",
      "cityid": "101010100",
      "temperature": "19℃/15℃",
      "humidity": "0%/0%",
      "weather": "小雨转中雨",
      "weather_icon": "http://api.k780.com/upload/weather/d/7.gif",
      "weather_icon1": "http://api.k780.com/upload/weather/n/8.gif",
      "wind": "西南风转北风",
      "winp": "小于3级转小于3级",
      "temp_high": "19",
      "temp_low": "15",
      "humi_high": "0",
      "humi_low": "0",
      "weatid": "8",
      "weatid1": "9",
      "windid": "5",
      "winpid": "0",
      "weather_iconid": "7",
      "weather_iconid1": "8"
    },
    {
      "weaid": "1",
      "days": "2021-09-20",
      "week": "星期一",
      "cityno": "beijing",
      "citynm": "北京",
      "cityid": "101010100",
      "temperature": "26℃/16℃",
      "humidity": "0%/0%",
      "weather": "多云转晴",
      "weather_icon": "http://api.k780.com/upload/weather/d/1.gif",
      "weather_icon1": "http://api.k780.com/upload/weather/n/0.gif",
      "wind": "北风",
      "winp": "3-4级转3-4级",
      "temp_high": "26",
      "temp_low": "16",
      "humi_high": "0",
      "humi_low": "0",
      "weatid": "2",
      "weatid1": "1",
      "windid": "8",
      "winpid": "1",
      "weather_iconid": "1",
      "weather_iconid1": "0"
    },
    {
      "weaid": "1",
      "days": "2021-09-21",
      "week": "星期二",
      "cityno": "beijing",
      "citynm": "北京",
      "cityid": "101010100",
      "temperature": "27℃/16℃",
      "humidity": "0%/0%",
      "weather": "晴",
      "weather_icon": "http://api.k780.com/upload/weather/d/0.gif",
      "weather_icon1": "http://api.k780.com/upload/weather/n/0.gif",
      "wind": "西北风转北风",
      "winp": "小于3级",
      "temp_high": "27",
      "temp_low": "16",
      "humi_high": "0",
      "humi_low": "0",
      "weatid": "1",
      "weatid1": "1",
      "windid": "7",
      "winpid": "0",
      "weather_iconid": "0",
      "weather_iconid1": "0"
    },
    {
      "weaid": "1",
      "days": "2021-09-22",
      "week": "星期三",
      "cityno": "beijing",
      "citynm": "北京",
      "cityid": "101010100",
      "temperature": "26℃/18℃",
      "humidity": "0%/0%",
      "weather": "多云",
      "weather_icon": "http://api.k780.com/upload/weather/d/1.gif",
      "weather_icon1": "http://api.k780.com/upload/weather/n/1.gif",
      "wind": "北风转东北风",
      "winp": "小于3级",
      "temp_high": "26",
      "temp_low": "18",
      "humi_high": "0",
      "humi_low": "0",
      "weatid": "2",
      "weatid1": "2",
      "windid": "8",
      "winpid": "0",
      "weather_iconid": "1",
      "weather_iconid1": "1"
    },
    {
      "weaid": "1",
      "days": "2021-09-23",
      "week": "星期四",
      "cityno": "beijing",
      "citynm": "北京",
      "cityid": "101010100",
      "temperature": "24℃/16℃",
      "humidity": "0%/0%",
      "weather": "多云",
      "weather_icon": "http://api.k780.com/upload/weather/d/1.gif",
      "weather_icon1": "http://api.k780.com/upload/weather/n/1.gif",
      "wind": "东北风",
      "winp": "小于3级",
      "temp_high": "24",
      "temp_low": "16",
      "humi_high": "0",
      "humi_low": "0",
      "weatid": "2",
      "weatid1": "2",
      "windid": "1",
      "winpid": "0",
      "weather_iconid": "1",
      "weather_iconid1": "1"
    }
  ]
}

2.2 json语法介绍

json里就分为两种结构: 对象和数组,通过这两种结构可以表示各种复杂的结构。

json语法规则
1. 大括号 { } 用来保存对象
2. 中括号 [ ] 用来保存数组,数组里也可以包含多个对象,对象里又可以包含数组,可以嵌套
3. json的值表示语法: key : value --> "width": 1280
4. 多个数据由逗号分隔: {"width": 1920,"height": 1080}

json值可以是以下几种类型:
1. 数字(整数或浮点数)
2. 字符串(在双引号中)
3. 逻辑值(true 或 false)
4. 数组(在中括号中)
5. 对象(在大括号中)
6. null (空值)

三、cjson创建简单json数据并解析

3.1 新建工程

这是下载下来的cjson源文件,将它加到自己工程中即可。

C/C++中CJSON的使用(创建与解析JSON数据)

我这里使用vs2017建立工程,演示实例。

C/C++中CJSON的使用(创建与解析JSON数据)

建好工程之后,将文件添加到工程里:

C/C++中CJSON的使用(创建与解析JSON数据)

在vs2017里使用c语言的字符串处理函数会报错,提示不安全;

1>d:\linux-share-dir\vs2017\console_cjsontest\console_cjsontest\cjson.c(155): error c4996: 'strcpy': this function or variable may be unsafe. consider using strcpy_s instead. to disable deprecation, use _crt_secure_no_warnings. see online help for details.

解决办法是:找到【项目属性】,点击【c++】里的【预处理器】,对【预处理器】进行编辑,在里面加入一段代码:_crt_secure_no_warnings。

C/C++中CJSON的使用(创建与解析JSON数据)

3.2 创建json数据

接下来目标是使用cjson创建出下面这样一个json格式数据:

?
1
2
3
4
5
6
7
{
  "text": "我是一个字符串数据",
  "number": 666,
  "state1": false,
  "state2": true,
  "state3": null
}

示例代码如下:

C/C++中CJSON的使用(创建与解析JSON数据)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
 
//因为当前工程使用的是cpp后缀文件,引用c语言的文件需要使用下面的这种方式
extern "c" {
#include <string.h>
#include <stdio.h>
#include "cjson.h"
}
 
int main()
{
    //1. 创建cjson对象
    cjson* root = cjson_createobject();
    
    //2. 创建数据
    cjson_addstringtoobject(root, "text","我是一个字符串数据");
    cjson_addnumbertoobject(root,"number",666);
    cjson_addbooltoobject(root, "state1", cjson_false);
    cjson_addbooltoobject(root, "state2", cjson_true);
    cjson_addnulltoobject(root, "state3");
 
    //3. 打印生成的结果
    char *json_data=cjson_printunformatted(root);
    printf("%s\n",json_data);
 
    //4. 释放空间
    cjson_delete(root);
    return 0;
}

C/C++中CJSON的使用(创建与解析JSON数据)

3.3 解析json数据

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
 
//因为当前工程使用的是cpp后缀文件,引用c语言的文件需要使用下面的这种方式
extern "c" {
#include <string.h>
#include <stdio.h>
#include "cjson.h"
}
 
//将要解析的json数据.
char data[] =
"{"
    "\"text\": \"我是一个字符串数据\","
    "\"number\" : 666,"
    "\"state1\" : false,"
    "\"state2\" : true,"
    "\"state3\" : null"
"}";
 
 
int main()
{
    //1. 载入json数据
    cjson* root = cjson_parse(data);
    if (root == null)return 0;
 
    //2. 解析字段
    cjson* item;
 
    item=cjson_getobjectitem(root,"text");
    if (item)
    {
        printf("text=%s\n",item->valuestring);
    }
 
    item = cjson_getobjectitem(root, "number");
    if (item)
    {
        printf("text=%d\n", item->valueint);
    }
 
    item = cjson_getobjectitem(root, "state1");
    if (item)
    {
        printf("state1=%d\n", item->valueint);
    }
 
    item = cjson_getobjectitem(root, "state2");
    if (item)
    {
        printf("state2=%d\n", item->valueint);
    }
 
    item = cjson_getobjectitem(root, "state3");
    if (item)
    {
        printf("state3=%d\n", item->valueint);
    }
 
    //3. 释放空间
    cjson_delete(root);
    return 0;
}

C/C++中CJSON的使用(创建与解析JSON数据)

四、cjson创建嵌套的对象数据

目标: 使用cjson创建出下面这样一个json格式数据

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
  "data1": {
    "text": "我是一个字符串数据1",
    "number": 666,
    "state1": false,
    "state2": true,
    "state3": null
  },
  "data2": {
    "text": "我是一个字符串数据2",
    "number": 666,
    "state1": false,
    "state2": true,
    "state3": null
  }
}

4.1 创建json数据

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
 
//因为当前工程使用的是cpp后缀文件,引用c语言的文件需要使用下面的这种方式
extern "c" {
#include <string.h>
#include <stdio.h>
#include "cjson.h"
}
 
int main()
{
    //1. 创建cjson对象
    cjson* root = cjson_createobject();
    
    //2. 创建对象数据1
    cjson* item1 = cjson_createobject();
    cjson_addstringtoobject(item1, "text","我是一个字符串数据1");
    cjson_addnumbertoobject(item1,"number",666);
    cjson_addbooltoobject(item1, "state1", cjson_false);
    cjson_addbooltoobject(item1, "state2", cjson_true);
    cjson_addnulltoobject(item1, "state3");
    cjson_additemtoobject(root, "data1", item1);
 
    //3. 创建对象数据2
    cjson* item2 = cjson_createobject();
    cjson_addstringtoobject(item2, "text", "我是一个字符串数据2");
    cjson_addnumbertoobject(item2, "number", 666);
    cjson_addbooltoobject(item2, "state1", cjson_false);
    cjson_addbooltoobject(item2, "state2", cjson_true);
    cjson_addnulltoobject(item2, "state3");
    cjson_additemtoobject(root, "data2", item2);
 
    //3. 打印生成的结果
    char *json_data=cjson_printunformatted(root);
    printf("%s\n",json_data);
 
    //4. 释放空间
    cjson_delete(root);
    return 0;
}

4.2 解析json数据

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
 
//因为当前工程使用的是cpp后缀文件,引用c语言的文件需要使用下面的这种方式
extern "c" {
#include <string.h>
#include <stdio.h>
#include "cjson.h"
}
 
//将要解析的json数据.
char data[] =
"{"
    "\"data1\": {"
    "\"text\": \"我是一个字符串数据1\","
    "\"number\" : 666,"
    "\"state1\" : false,"
    "\"state2\" : true,"
    "\"state3\" : null"
     "},"
    "\"data2\": {"
    "\"text\":\"我是一个字符串数据2\","
    "\"number\" : 666,"
    "\"state1\" : false,"
    "\"state2\" : true,"
    "\"state3\" : null"
    "}"
"}";
 
int main()
{
    //1. 载入json数据
    cjson* root = cjson_parse(data);
    if (root == null)return 0;
 
    //2. 解析字段
    cjson* item;
 
    item=cjson_getobjectitem(root,"data1");
    if (item)
    {
        cjson *obj;
        obj=cjson_getobjectitem(item, "text");
        if (obj)
        {
            printf("text=%s\n", obj->valuestring);
        }
 
        obj=cjson_getobjectitem(item, "number");
        if (obj)
        {
            printf("number=%d\n", obj->valueint);
        }
 
        obj = cjson_getobjectitem(item, "state1");
        if (obj)
        {
            printf("state1=%d\n", obj->valueint);
        }
 
        obj = cjson_getobjectitem(item, "state2");
        if (obj)
        {
            printf("state2=%d\n", obj->valueint);
        }
 
        obj = cjson_getobjectitem(item, "state3");
        if (obj)
        {
            printf("state3=%d\n", obj->valueint);
        }
    }
 
    item = cjson_getobjectitem(root, "data2");
    if (item)
    {
        cjson *obj;
        obj = cjson_getobjectitem(item, "text");
        if (obj)
        {
            printf("text=%s\n", obj->valuestring);
        }
 
        obj = cjson_getobjectitem(item, "number");
        if (obj)
        {
            printf("number=%d\n", obj->valueint);
        }
 
        obj = cjson_getobjectitem(item, "state1");
        if (obj)
        {
            printf("state1=%d\n", obj->valueint);
        }
 
        obj = cjson_getobjectitem(item, "state2");
        if (obj)
        {
            printf("state2=%d\n", obj->valueint);
        }
 
        obj = cjson_getobjectitem(item, "state3");
        if (obj)
        {
            printf("state3=%d\n", obj->valueint);
        }
    }
 
    //3. 释放空间
    cjson_delete(root);
    return 0;
}

C/C++中CJSON的使用(创建与解析JSON数据)

五、cjson带数组的json数据

目标: 使用cjson创建出下面这样一个json格式数据

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
  "text": [
    {
      "width": 1280,
      "height": 720
    },
    {
      "width": 1920,
      "height": 1080
    },
    {
      "width": 3840,
      "height": 2160
    }
  ]
}

5.1 创建json数据

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
 
//因为当前工程使用的是cpp后缀文件,引用c语言的文件需要使用下面的这种方式
extern "c" {
#include <string.h>
#include <stdio.h>
#include "cjson.h"
}
 
int main()
{
    cjson *width = null;
    cjson *height = null;
    int i;
    const unsigned int resolution_numbers[3][2] = {
        {1280, 720},
        {1920, 1080},
        {3840, 2160}
    };
 
    //1. 创建cjson对象
    cjson* root = cjson_createobject();
    
    //2. 创建数组对象
    cjson *array = cjson_createarray();
    cjson_additemtoobject(root, "text", array);
 
    for (i = 0; i < (sizeof(resolution_numbers) / (2 * sizeof(int))); ++i)
    {
        cjson *obj = cjson_createobject();
        cjson_additemtoarray(array, obj);
 
        width = cjson_createnumber(resolution_numbers[i][0]);
        cjson_additemtoobject(obj, "width", width);
 
        height = cjson_createnumber(resolution_numbers[i][1]);
        cjson_additemtoobject(obj, "height", height);
    }
 
    //3. 打印生成的结果
    char *json_data=cjson_printunformatted(root);
    printf("%s\n",json_data);
 
    //4. 释放空间
    cjson_delete(root);
    return 0;
}

5.2 解析json数据

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
 
//因为当前工程使用的是cpp后缀文件,引用c语言的文件需要使用下面的这种方式
extern "c" {
#include <string.h>
#include <stdio.h>
#include "cjson.h"
}
 
//将要解析的json数据.
char data[] =
"{"
"\"text\": ["
"{"
"\"width\": 1280,"
"\"height\" : 720"
"},"
"{"
"\"width\": 1920,"
"\"height\" : 1080"
"},"
"{"
"\"width\": 3840,"
"\"height\" : 2160"
"}"
"]"
"}";
 
int main()
{
    //1. 载入json数据
    cjson* root = cjson_parse(data);
    if (root == null)return 0;
    //2. 解析字段
    cjson* item;
    int i;
    item = cjson_getobjectitem(root, "text");
    if (item)
    {
        //获取数组的大小
        int arraysize = cjson_getarraysize(item);
        //解析数组的里的每个成员
        for (i = 0; i < arraysize; i++)
        {
            //取出数组下标对象
            cjson *array_item = cjson_getarrayitem(item, i);
            if (array_item == null)continue;
 
            //解析数据
            cjson *obj = cjson_getobjectitem(array_item, "width");
            if (obj)
            {
                printf("width=%d\n",obj->valueint);
            }
            obj = cjson_getobjectitem(array_item, "height");
            if (obj)
            {
                printf("height=%d\n", obj->valueint);
            }
        }
    }
    
    //3. 释放空间
    cjson_delete(root);
    return 0;
}

C/C++中CJSON的使用(创建与解析JSON数据)

到此这篇关于c/c++中cjson的使用(创建与解析json数据)的文章就介绍到这了,更多相关c++ cjson使用内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/xiaolong1126626497/article/details/120345217

延伸 · 阅读

精彩推荐