我是靠谱客的博主 碧蓝紫菜,这篇文章主要介绍Linux系统下如何使用C++解析json文件详解,现在分享给大家,希望可以做个参考。

1. 背景

工作需要,下班回来自己造轮子,记录以后查阅。

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,和xml类似,本文主要Linux下使用Jsoncpp解析json的方法做一下记录。

2. 关于jsoncpp库的使用简介

使用jsoncpp有两种方法

方法一:使用Jsoncpp生成的lib文件

      解压上面下载的Jsoncpp文件,在jsoncpp默认生成静态链接库。 在工程中引用,只需要包含include/json下的头文件及生成的.lib文件即可。

方法二:使用Jsoncpp包中的.cpp和.h文件

      解压上面下载的Jsoncpp文件,把jsoncpp-src-0.5.0文件拷贝到工程目录下,将jsoncpp-src-0.5.0jsoncpp-src-0.5.0includejson和jsoncpp-src-0.5.0jsoncpp-src-0.5.0srclib_json目录里的文件包含到VS工程中,在VS工程的属性C/C++下General中Additional Include Directories包含头文件目录.jsoncpp-src-0.5.0include。在使用的cpp文件中包含json头文件即可,如:#include "json/json.h"。将json_reader.cpp、json_value.cpp和json_writer.cpp三个文件的Precompiled Header属性设置为Not Using Precompiled Headers,否则编译会出现错误。

我上面的都是采用第一种方法

jsoncpp 使用详解

jsoncpp 主要包含三种类型的 class:Value、Reader、Writer。jsoncpp 中所有对象、类名都在 namespace Json 中,包含 json.h 即可。

Json::Value 只能处理 ANSI 类型的字符串,如果 C++ 程序是用 Unicode 编码的,最好加一个 Adapt 类来适配。

3. linux下jsoncpp环境配置

3.1 jsoncpp源码下载

直接在github搜索jsoncpp即可。给出下载连接:https://github.com/open-source-parsers/jsoncpp

3.2 具体配置步骤

1> 解压源码

2> 在源码目录的上一层新建build目录,用来保存编译过程生成的中间文件

3> 在build目录执行cmake ..

4> 在build目录执行make

5> 在build目录下执行make install

查看安装在本地的jsoncpp库

ls /usr/local/include     ls/usr/local/lib

4. 使用c++读取json文件示例

demo.json文件【源码直接抄录自https://blog.csdn.net/shuiyixin/article/details/89330529】

复制代码
1
2
3
4
5
6
7
8
9
10
11
{ "age" : 21, "friends" : { "friend_age" : 21, "friend_name" : "ZhaoWuxian", "friend_sex" : "man" }, "hobby" : [ "sing", "run", "Tai Chi" ], "name" : "shuiyixin", "sex" : "man" }

test.cpp文件

复制代码
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
#include <iostream> #include <json/json.h> #include <fstream> using namespace std; void readFileJson() { Json::Reader reader; Json::Value root; //从文件中读取,保证当前文件有demo.json文件 ifstream in("demo.json", ios::binary); if (!in.is_open()) { cout << "Error opening filen"; return; } if (reader.parse(in, root)) { //读取根节点信息 string name = root["name"].asString(); int age = root["age"].asInt(); string sex = root["sex"].asString(); cout << "My name is " << name << endl; cout << "I'm " << age << " years old" << endl; cout << "I'm a " << sex << endl; //读取子节点信息 string friend_name = root["friends"]["friend_name"].asString(); int friend_age = root["friends"]["friend_age"].asInt(); string friend_sex = root["friends"]["friend_sex"].asString(); cout << "My friend's name is " << friend_name << endl; cout << "My friend's sex is "<<friend_sex << endl; cout << "My friend is " << friend_age << " years old" << endl; //读取数组信息 cout << "Here's my hobby:" << endl; for (unsigned int i = 0; i < root["hobby"].size(); i++) { string ach = root["hobby"][i].asString(); cout << ach << 't'; } cout << endl; cout << "Reading Complete!" << endl; } else { cout << "parse errorn" << endl; } in.close(); } int main(void) { readFileJson(); return 0; }

执行结果如下:

5. 使用c++写入json文件示例

test.cpp文件:

复制代码
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
#include <iostream> #include <json/json.h> #include <fstream> using namespace std; void writeFileJson() { //根节点 Json::Value root; //根节点属性 root["name"] = Json::Value("shuiyixin"); root["age"] = Json::Value(21); root["sex"] = Json::Value("man"); //子节点 Json::Value friends; //子节点属性 friends["friend_name"] = Json::Value("ZhaoWuxian"); friends["friend_age"] = Json::Value(21); friends["friend_sex"] = Json::Value("man"); //子节点挂到根节点上 root["friends"] = Json::Value(friends); //数组形式 root["hobby"].append("sing"); root["hobby"].append("run"); root["hobby"].append("Tai Chi"); //直接输出 //cout << "FastWriter:" << endl; //Json::FastWriter fw; //cout << fw.write(root) << endl << endl; //缩进输出 cout << "StyledWriter:" << endl; Json::StyledWriter sw; cout << sw.write(root) << endl << endl; //输出到文件 ofstream os; os.open("demo.json", std::ios::out | std::ios::app); if (!os.is_open()) cout << "error:can not find or create the file which named " demo.json"." << endl; os << sw.write(root); os.close(); } int main(void) { writeFileJson(); return 0; }

执行结果如下:可以看到已经在目录新建了demo.json文件,并且写入成功

要注意的是:

1.如果要写入的文件不存在,会自动创建该文件;

2.如果文件存在,写入过程不会覆盖文件中原有数据,而是将新数据写在原有数据后面。

6. 从字符串解析json

在实际项目中更多使用的是从文件解析json,从字符串解析json示例只是为了完整记录。

6.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
#include <iostream> #include <json/json.h> #include <fstream> using namespace std; void readStrJson() { //字符串 const char* str = "{"name":"shuiyixin","age":"21","sex":"man"}"; // "{ // "name" : "shuiyixin", // "age" : "21", // "sex" : "man" // }"; Json::Reader reader; Json::Value root; //从字符串中读取数据 if (reader.parse(str, root)) { string name = root["name"].asString(); int age = root["nomen"].asInt(); string sex = root["sex"].asString(); cout << name + "," << age << "," << sex << endl; } } int main(void) { readStrJson(); return 0; }

执行结果如下:

6.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
#include <iostream> #include <json/json.h> #include <fstream> using namespace std; void readStrProJson() { string strValue = "{"name":"shuiyixin","major":[{"AI":"MachineLearning"},{"AI":"DeepLearning"},{"AI":"ComputerVision"}]}"; /* { "name":"shuiyixin", "major":[ { "AI":"MachineLearning" }, { "AI":"DeepLearning" }, { "AI":"ComputerVision" }] } */ Json::Reader reader; Json::Value value; if (reader.parse(strValue, value)) { string out = value["name"].asString(); cout << out << endl; const Json::Value arrayObj = value["major"]; for (unsigned int i = 0; i < arrayObj.size(); i++) { out = arrayObj[i]["AI"].asString(); cout << out<<endl; } } } int main(void) { readStrProJson(); return 0; }

执行结果如下:

参看连接:

https://www.uoften.com/article/214039.htm

https://www.uoften.com/article/214051.htm

总结

到此这篇关于Linux系统下如何使用C++解析json文件的文章就介绍到这了,更多相关Linux C++解析json文件内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是碧蓝紫菜最近收集整理的关于Linux系统下如何使用C++解析json文件详解的全部内容,更多相关Linux系统下如何使用C++解析json文件详解内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部