
json-c库
json-c 库中是在嵌入式开发中常用的库。
因为很多地方都以json数据数据交互协议, 尤其嵌入式web数据交互时通常会用到json格式, 因此如果需要在产品端进行json数据解析 , json-c 是一个比较不错的选择。
API接口
json-c库中有一个json对象 :jsonobj
.它会将一个json文件解析为一个json对象.
使用方式:
1.解析json文件,获取一个解析后的json对象.
2.访问对应的key值.
3.使用后,释放json对象.
解析json文件
方式一:将json格式的字符串转成json对象
复制代码
1json_object* json_tokener_parse(const char *str);
方式二:具有json格式文本内容的文本文件转化为json对象
复制代码
1json_object* json_object_from_file(const char *filename);
访问对应的key值
复制代码
1extern json_bool json_object_object_get_ex(struct json_object* obj,const char *key,struct json_object **value);
释放json文件
复制代码
1Void json_object_put(struct json_object * this);
其他常用API
(1)创建一个空的json_type_object类型JSON对象:
复制代码
1struct json_object * json_object_new_object();
(2)创建一个空的json_type_array类型JSON数组值对象:
复制代码
1struct json_object * json_object_new_array();
(3)从json中按名字取一个对象:
复制代码
1struct json_object * json_object_object_get(struct json_object * json,char *name);
(4)减少对象引用次数一次,当减少到0就释放(free)资源:
复制代码
1Void json_object_put(struct json_object * this);
(5)将json_object内容转换json格式字符串,其中可能含有转义符:
复制代码
1char * json_object_to_json_string(struct json_object * this);
(6)添加一个对象域到json对象中:
复制代码
1void json_object_object_add(struct json_object* obj, char *key, struct json_object *val);
(7)删除key值json对象:
复制代码
1void json_object_object_del(struct json_object* obj, char *key);
(8)得到json对象数组的长度:
复制代码
1int json_object_array_length(struct json_object *obj);
(9)添加一元素在json对象数组末端:
复制代码
1extern int json_object_array_add(struct json_object *obj, struct json_object *val);
(10)在指定的json对象数组下标插入或替换一个json对象元素:
复制代码
1int json_object_array_put_idx(struct json_object *obj, int idx, struct json_object *val);
(11)从数组中,按下标取JSON值对象:
复制代码
1struct json_object * json_object_array_get_idx(struct json_object * json_array,int i);
(12)得到json_object的类型:
复制代码
1enum json_type json_object_get_type(struct json_object * this )
参考代码[转载]
复制代码
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 <stdio.h> #include <json-c/json.h> /* { Name: haha, Id: 101, Age: 21, info:{ number: 1, score: 91, type: 2, params: "" } } */ // gcc json-c_parse_string.c -ljson-c int main(int argc, char const *argv[]) { /* Declaring the json data's in json format. */ char buf[] = "{ "Name": "haha", "Id": 101, "Age": 21, "info": { "number": 1, "score": 91, "type": 2, "params": "w" } }"; /* Declaring the json_object. To pass the Json string to the newly created json_object. */ json_object *new_obj = json_tokener_parse(buf); if (!new_obj) return -1; json_object *val_obj = NULL; json_object *result = NULL; const char *str = NULL; /* To get the data's then we have to get to the specific node by using the below function. */ if( json_object_object_get_ex(new_obj, "Name", &val_obj) ) { str = json_object_get_string(val_obj); printf("Name: %sn", str); } if( json_object_object_get_ex(new_obj, "Id", &val_obj) ) { str = json_object_get_string(val_obj); printf("Id: %sn", str); } if( json_object_object_get_ex(new_obj, "Age", &val_obj) ) { str = json_object_get_string(val_obj); printf("Age: %sn", str); } if( json_object_object_get_ex(new_obj, "info", &val_obj) ) { if( json_object_object_get_ex(val_obj, "number", &result) ) { printf("tinfo -> number: %dn", json_object_get_int(result)); } if( json_object_object_get_ex(val_obj, "score", &result) ) { printf("tinfo -> score: %dn", json_object_get_int(result)); } if( json_object_object_get_ex(val_obj, "type", &result) ) { printf("tinfo -> type: %dn", json_object_get_int(result)); } if( json_object_object_get_ex(val_obj, "params", &result) ) { printf("tinfo -> params: %sn", json_object_get_string(result)); } } json_object_put(new_obj); // to return the pointer to its originalobjects return 0; }
参考
使用json-c 体会LINUX下C语言操作JSON数据JSON C语言API整理
最后
以上就是感性白云最近收集整理的关于Android下json-c库使用的全部内容,更多相关Android下json-c库使用内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复