我是靠谱客的博主 苹果钻石,这篇文章主要介绍[JSON-C库的使用] 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
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include "json.h" void encodeJson(); void decodeJson(); int main(int argc, char **argv) { int ret = 0; encodeJson(); decodeJson(); return ret; } static char *json_type_to_name(int json_type) { char json_type_name[128]; memset(json_type_name, 0x00, sizeof(json_type_name)); switch(json_type) { case json_type_null: snprintf(json_type_name, sizeof(json_type_name), "%s", "json_type_null"); break; case json_type_boolean: snprintf(json_type_name, sizeof(json_type_name), "%s", "json_type_boolean"); break; case json_type_double: snprintf(json_type_name, sizeof(json_type_name), "%s", "json_type_double"); break; case json_type_int: snprintf(json_type_name, sizeof(json_type_name), "%s", "json_type_int"); break; case json_type_object: snprintf(json_type_name, sizeof(json_type_name), "%s", "json_type_object"); break; case json_type_array: snprintf(json_type_name, sizeof(json_type_name), "%s", "json_type_array"); break; case json_type_string: snprintf(json_type_name, sizeof(json_type_name), "%s", "json_type_string"); break; } return json_type_name; } static void getFieldValue(struct json_object *child_obj, const char *field) { struct json_object *obj = json_object_object_get(child_obj, field); enum json_type obj_type = json_object_get_type(obj); //printf("%s json_type is %sn", field, json_type_to_name(obj_type)); if (obj_type == json_type_int) { printf("%s=%dn", field, json_object_get_int(obj)); } else if (obj_type == json_type_string) { printf("%s=%sn", field, json_object_get_string(obj)); } } void decodeJson() { const char *json_string = "{ n "userinfo": [ n { "pin": "10000", "cardno": "123456789", "password": "123456", "name": "paul", "Privilege": 14, "flag": 1 }, n { "pin": "10001", "cardno": "123456789", "password": "123456", "name": "paul", "Privilege": 14, "flag": 1 }, n { "pin": "10002", "cardno": "123456789", "password": "123456", "name": "paul", "Privilege": 14, "flag": 1 }, n { "pin": "10003", "cardno": "123456789", "password": "123456", "name": "paul", "Privilege": 14, "flag": 1 }, n { "pin": "10004", "cardno": "123456789", "password": "123456", "name": "paul", "Privilege": 14, "flag": 1 }, n { "pin": "10005", "cardno": "123456789", "password": "123456", "name": "paul", "Privilege": 14, "flag": 1 }, n { "pin": "10006", "cardno": "123456789", "password": "123456", "name": "paul", "Privilege": 14, "flag": 1 }, n { "pin": "10007", "cardno": "123456789", "password": "123456", "name": "paul", "Privilege": 14, "flag": 1 }, n { "pin": "10008", "cardno": "123456789", "password": "123456", "name": "paul", "Privilege": 14, "flag": 1 }, n { "pin": "10009", "cardno": "123456789", "password": "123456", "name": "paul", "Privilege": 14, "flag": 1 } n ] }"; int ret = 0; int i = 0; struct json_object *root_obj = NULL; struct json_object *arry_obj = NULL; struct json_object *child_obj = NULL; struct lh_entry *entry = NULL; char *key = NULL; root_obj = json_tokener_parse(json_string); entry = json_object_get_object(root_obj)->head; if (entry) { key = (char *)entry->k; arry_obj = (struct json_object *)entry->v; if (json_type_array != json_object_get_type(arry_obj) && json_type_object != json_object_get_type(arry_obj)) { return -1; } printf("key = %sn", key); printf("array_obj = %sn", json_object_to_json_string(arry_obj)); if (json_type_array == json_object_get_type(arry_obj)) { for(i = 0; i < json_object_array_length(arry_obj); i++) { child_obj = json_object_array_get_idx(arry_obj, i); //printf("child_obj = %sn", json_object_to_json_string(child_obj)); getFieldValue(child_obj, "pin"); getFieldValue(child_obj, "cardno"); getFieldValue(child_obj, "password"); getFieldValue(child_obj, "name"); getFieldValue(child_obj, "Privilege"); getFieldValue(child_obj, "flag"); printf("n"); } } else if (json_type_object == json_object_get_type(arry_obj)) { } } } void encodeJson() { int i = 0; struct json_object *root_obj = NULL; struct json_object *child_obj = NULL; struct json_object *array_obj = NULL; char *tmpStr = NULL; root_obj = json_object_new_object(); array_obj = json_object_new_array(); for(i = 0; i < 10; i++) { child_obj = json_object_new_object(); json_object_object_add(child_obj, "pin", json_object_new_string("10000")); json_object_object_add(child_obj, "cardno", json_object_new_string("123456789")); json_object_object_add(child_obj, "password", json_object_new_string("123456")); json_object_object_add(child_obj, "name", json_object_new_string("paul")); json_object_object_add(child_obj, "Privilege", json_object_new_int(14)); json_object_object_add(child_obj, "flag", json_object_new_int(1)); json_object_array_add(array_obj, child_obj); child_obj = NULL; } json_object_object_add(root_obj, "userinfo", array_obj); tmpStr = json_object_to_json_string(root_obj); printf("%sn", tmpStr); }

最后

以上就是苹果钻石最近收集整理的关于[JSON-C库的使用] Json对象数组的解析的全部内容,更多相关[JSON-C库的使用]内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(97)

评论列表共有 0 条评论

立即
投稿
返回
顶部