我是靠谱客的博主 鲜艳鸡,这篇文章主要介绍【Json】Linux 下C/C++解析JSON文件,现在分享给大家,希望可以做个参考。

Linux 下C/C++解析JSON文件

      • 安装CMAKE
      • 安装JSON-C
      • 代码示例
        • 写入JSON格式示例
        • 读入JSON格式示例

安装CMAKE

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 安装gcc等必备程序包 # yum install -y gcc gcc-c++ make automake # 安装wget # yum install -y wget # 获取CMake源码包 # wget https://github.com/Kitware/CMake/releases/download/v3.22.2/cmake-3.22.2.tar.gz # 解压CMake源码包 # tar -zxvf cmake-3.22.2.tar.gz # 进入目录 # cd cmake-3.22.2.tar.gz # 编译前准备工作 # ./bootstrap # 编译 # gmake # 进行安装 # gmake install

安装JSON-C

简介

JSON库多种多样,但是JSON-C由于兼容性好,支持UTF-8,所以使用比较广泛。

就json来说,由于结构比较简单,不用库也是可以的。

但json-c提供了超出json范围的一些功能,实际上完成了数据序列化和反序列化,数据的存储和检索,数据对象化等功能。还是非常有使用价值的。

https://github.com/json-c/json-c/wiki

官方API手册 : http://json-c.github.io/json-c/json-c-0.13.1/doc/html/index.html

http://zengriguang.blog.163.com/blog/static/17076248720121080187635/

安装JSON-C方法一

复制代码
1
2
# yum install json-c json-c-devel

安装JSON-C方法二

复制代码
1
2
3
4
5
6
7
8
9
# 下载源码 # git clone https://github.com/json-c/json-c.git # 安装 # cd json-c # cmake ./CMakeLists.txt # make # make intsall

代码示例

写入JSON格式示例

复制代码
1
2
3
4
5
# mkdir json_test # cd json_test # touch test_write.c # vi test_write.c

test.c

复制代码
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
#include <stdio.h> #include <string.h> #include <json.h> int main(int argc,char **argv) { json_object *json=json_object_new_object(); json_object_object_add(json, "name",json_object_new_string("laomeng")); json_object_object_add(json, "email",json_object_new_string("laomeng188@163.com")); json_object_object_add(json, "age",json_object_new_int(30)); json_object *tech=json_object_new_array(); json_object_array_add(tech,json_object_new_string("c")); json_object_array_add(tech,json_object_new_string("c++")); json_object_array_add(tech,json_object_new_string("php")); json_object_object_add(json,"technology",tech); const char *str=json_object_to_json_string(json); printf("%sn",str); json_object_to_file("data_write.json", json); json_object_put(json); return 0; }

编译

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 拷贝已编译好了的源码目录json-c到我们的测试目录 # cp json-c ./json_test # 查看目录下的文件 # cd ../json_test/ # ls json-c test_write.c # 编译 ## gcc指令说明: ### 处理动态链接库时有2个路径:链接时路径和运行时路径,2个路径是分开的。因此gcc编译链接动态库时,很有可能编译通过,但是执行时,找不到动态链接库 ### -L选项指定的路径只在编译时有效,编译出来的可执行文件不知道-L选项后面的值,可以使用ldd <your_execute>看看是不有 ‘not found’在你链接的库后面 ### 后面的-Wl,-rpath=./json-c的意思是让编译运行时可以找到对应的库目录路径(这里是./json-c) # gcc -o test_write test_write.c -ljson-c -I./json-c -L./json-c -Wl,-rpath=./json-c # 查看目录下的文件 # ls json-c test_write test_write.c data_write.json

运行

复制代码
1
2
# ./test_write

读入JSON格式示例

复制代码
1
2
3
4
# cd json_test # touch test_read.c # vi test_read.c

test_read.c

复制代码
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
#include<json.h> #include<json_object_private.h> #include<stdio.h> void printf_json_object_string(json_object *obj, const char* obj_name) { json_object *o = json_object_object_get(obj, obj_name); const char *str = json_object_to_json_string(o); printf("count:%dttype:%dt%sn",o->_ref_count,o->o_type,str); obj_name = NULL; str = NULL; } void print_json_object_kv_string(json_object *obj, const char* obj_name) { struct lh_table *lt = json_object_get_object(obj); struct lh_entry *en = lh_table_lookup_entry(lt, obj_name); json_object *kobj = (json_object*)(en->k); json_object *vobj = (json_object*)(en->v); printf("%sn", lh_entry_k(en)); printf("%sn", lh_entry_v(en)); // printf("%s, %sn",json_object_to_json_string(kobj), json_object_to_json_string(vobj)); json_object *o = json_object_object_get(obj, obj_name); struct lh_table *lt2 = json_object_get_object(o); struct lh_entry * entry; lh_foreach(lt, entry) printf("%s, %sn", entry->k, entry->v); } int main(int argc, char **argv) { //FILE* fid=fopen(argv[1],"r"); const char *str; json_object* json; json=json_object_from_file(argv[1]); str=json_object_to_json_string(json); // printf("count:%dntype:%dn%sn",json->_ref_count,json->o_type,str); // print_json_object_kv_string(json, "hero"); // print_json_object_kv_string(json, "glossary"); printf_json_object_string(json, "hero"); printf_json_object_string(json, "glossary"); json_object *ob = json_object_object_get(json, "glossary"); printf_json_object_string(ob, "title"); printf_json_object_string(ob, "GlossDiv"); json_object *ob1 = json_object_object_get(ob, "GlossDiv"); printf_json_object_string(ob1, "title"); printf_json_object_string(ob1, "GlossList"); json_object *ob2 = json_object_object_get(ob1, "GlossList"); // struct *array_list = json_object_get_array(ob2); size_t size = json_object_array_length(ob2); int idx = 0; for (idx = 0; idx < size; ++idx) { json_object *o = json_object_array_get_idx(ob2, idx); str=json_object_to_json_string(o); printf("count:%dttype:%dt%sn",o->_ref_count,o->o_type,str); printf_json_object_string(o, "ID"); printf_json_object_string(o, "SortAs"); printf_json_object_string(o, "GlossTerm"); printf_json_object_string(o, "Acronym"); printf_json_object_string(o, "Abbrev"); printf_json_object_string(o, "GlossDef"); json_object *ol = json_object_object_get(o, "GlossSeeAlso"); enum json_type type = json_object_get_type(ol); size_t size2 = json_object_array_length(ol); int idx2 = 0; for (idx2 = 0; idx2 < size2; ++idx2) { json_object *ol2 = json_object_array_get_idx(ol, idx2); str=json_object_to_json_string(ol2); printf("count:%dttype:%dt%sn",ol2->_ref_count,ol2->o_type,str); } } json_object_put(json); return 0; }

编译

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 拷贝已编译好了的源码目录json-c到我们的测试目录 # cp json-c ./json_test # 查看目录下的文件 # cd ../json_test/ # ls json-c test_read.c data_read.json # 编译 ## gcc指令说明: ### 处理动态链接库时有2个路径:链接时路径和运行时路径,2个路径是分开的。因此gcc编译链接动态库时,很有可能编译通过,但是执行时,找不到动态链接库 ### -L选项指定的路径只在编译时有效,编译出来的可执行文件不知道-L选项后面的值,可以使用ldd <your_execute>看看是不有 ‘not found’在你链接的库后面 ### 后面的-Wl,-rpath=./json-c的意思是让编译运行时可以找到对应的库目录路径(这里是./json-c) # gcc -o test_read test_read.c -ljson-c -I./json-c -L./json-c -Wl,-rpath=./json-c # 查看目录下的文件 # ls json-c test_read test_read.c data_read.json

运行

复制代码
1
2
# ./test_read data_read.json

最后

以上就是鲜艳鸡最近收集整理的关于【Json】Linux 下C/C++解析JSON文件的全部内容,更多相关【Json】Linux内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部