json-c的使用

json

JSON(JavaScript Object Notation, JS对象简谱)是一种轻量级的数据交换格式。它基于 ECMAScript(European Computer Manufacturers Association, 欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。

所以在有web或者app的项目中,前端很大概率会使用到json来传输数据,后端解析json有很多库可以选择,在C和C++中我使用不同的库,这次介绍在C中使用的json-c

数据类型

数据类型如下:

1
2
3
4
5
6
7
8
9
10
typedef enum json_type {
/* If you change this, be sure to update json_type_to_name() too */
json_type_null,
json_type_boolean,
json_type_double,
json_type_int,
json_type_object,
json_type_array,
json_type_string,
} json_type;

创建json

创建json的方法如下:

1
2
3
4
5
6
7
8
9
10
11
extern struct json_object* json_object_new_object(void);

extern struct json_object* json_tokener_parse(const char *str);

extern struct json_object* json_object_new_boolean(json_bool b);
extern struct json_object* json_object_new_double(double d);
extern struct json_object* json_object_new_int(int32_t i);
extern struct json_object* json_object_new_int64(int64_t i);
extern struct json_object* json_object_new_array(void);
extern struct json_object* json_object_new_string(const char *s);

  • json_object_new_object是创建一个json对象,对象内容为空
  • json_tokener_parse是根据json字符串创建json对象,对象根据字符串生成
  • 其余根据名称可以看出是创建不同的json对象

添加对象

添加对象的方法如下:

1
2
extern int json_object_object_add(struct json_object* obj, const char *key, struct json_object *val);
extern int json_object_array_add(struct json_object* obj, struct json_object *val);
  • obj:要被添加的json_object对象
  • key:字段名称
  • val:与key关联的json_object对象

解析json

解析json的方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
extern json_bool json_object_object_get_ex(struct json_object* obj, const char *key, struct json_object **value);

extern enum json_type json_object_get_type(struct json_object *obj);

extern const char* json_object_to_json_string(struct json_object *obj);

extern json_bool json_object_get_boolean(struct json_object *obj);
extern double json_object_get_double(struct json_object *obj);
extern int32_t json_object_get_int(struct json_object *obj);
extern int64_t json_object_get_int64(struct json_object *obj);
extern const char* json_object_get_string(struct json_object *obj);
extern int json_object_array_length(struct json_object *obj);
extern struct array_list* json_object_get_array(struct json_object *obj);

  • json_object_object_get_ex是根据key值获取一个json对象,返回值代表是否成功
  • json_object_get_type是判断json对象的类型
  • json_object_to_json_string是将json对象转换为字符串
  • 其余根据名称可以看出是根据key获取不同的value

释放空间

使用如下函数释放空间:

1
extern int json_object_put(struct json_object *obj);

使用创建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
#include <stdio.h>
#include <string.h>
#include <json-c/json.h>
#include <json-c/json_tokener.h>
#include <json-c/json_object.h>
#include <json-c/json_inttypes.h>
#include <json-c/json_util.h>

const char *json_get_string_value_by_field(struct json_object *json, const char *p_field)
{
struct json_object *string_json = NULL;

json_object_object_get_ex(json, p_field, &string_json);
if (NULL == string_json)
{
printf("json_object_object_get error %s", p_field);
return NULL;
}

if (json_type_string == json_object_get_type(string_json))
{
return json_object_get_string(string_json);
}

return NULL;
}

int json_get_int_value_by_field(struct json_object *json, const char *p_field)
{
struct json_object *int_json = NULL;

json_object_object_get_ex(json, p_field, &int_json);
if (NULL == int_json)
{
printf("json_object_object_get error %s", p_field);
return -1;
}

if (json_type_int == json_object_get_type(int_json))
{
return (int)json_object_get_int(int_json);
}

return -1;
}

const char *json_get_string_value(struct json_object *json)
{
if (json_type_string == json_object_get_type(json))
{
return json_object_get_string(json);
}

return NULL;
}

struct json_object *json_get_json_object_by_field(struct json_object *json, const char *p_field)
{
struct json_object *json_obj = NULL;

json_object_object_get_ex(json, p_field, &json_obj);
if (NULL == json_obj)
{
printf("json_object_object_get error %s", p_field);
return NULL;
}

return json_obj;
}

int json_is_array(struct json_object *json)
{
if (json_type_array == json_object_get_type(json))
{
return 0;
}

return -1;
}

void printFunc(struct json_object *Carl)
{
if (Carl == NULL)
{
return;
}

const char *name;
int age;
struct json_object *PhoneArray = NULL;
name = json_get_string_value_by_field(Carl, "name");
age = json_get_int_value_by_field(Carl, "age");
PhoneArray = json_get_json_object_by_field(Carl, "phone");

printf("name:%s\nage:%d\n", name, age);
printf("phone num:\n");

if (0 == json_is_array(PhoneArray))
{
for (int i = 0; i < json_object_array_length(PhoneArray); i++)
{
printf("%s\n", json_object_get_string(json_object_array_get_idx(PhoneArray, i)));
}
}
}

int main()
{
struct json_object *Carl = NULL;
struct json_object *Sam = NULL;
struct json_object *PhoneObject = NULL;
char *root_json = "{\"name\":\"Carl\",\"age\":24,\"phone\":[\"12345678901\",\"12345678902\"]}";

Carl = json_tokener_parse(root_json);

printFunc(Carl);

Sam = json_object_new_object();
if (Sam == NULL)
{
return -1;
}

json_object_object_add(Sam, "name", json_object_new_string("Sam"));
json_object_object_add(Sam, "age", json_object_new_int(21));

PhoneObject = json_object_new_array();
if (PhoneObject == NULL)
{
json_object_put(Sam);
return -1;
}
json_object_array_add(PhoneObject, json_object_new_string("12345678903"));
json_object_array_add(PhoneObject, json_object_new_string("12345678904"));
json_object_object_add(Sam, "phone", PhoneObject);

printFunc(Sam);

json_object_put(Carl);
json_object_put(Sam);
return 0;
}

/*
打印:
name:Carl
age:24
phone num:
12345678901
12345678902
name:Sam
age:21
phone num:
12345678903
12345678904
*/


json-c的使用
https://carl-5535.github.io/2022/10/19/工作总结/json-c的使用/
作者
Carl Chen
发布于
2022年10月19日
许可协议