软件系统定制开发在上一篇博客我讲解了使用C++读取XML文件,软件系统定制开发在这篇博客中我将讲解解析JSON文件。在解析JSON软件系统定制开发文件时我使用的解析库是,软件系统定制开发编译平台是VS2019。软件系统定制开发希望看完本篇博客能对你有所帮助。
JSON简介
在学习解析JSON文件之前,我们应该先了解什么是JSON。
JSON简介:
JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式。它基于 (欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
- JSON: JavaScript Object Notation(JavaScript 对象表示法)
- JSON 是存储和交换文本信息的语法,类似 XML。
- JSON 比 XML 更小、更快,更易解析。
- JSON 易于人阅读和编写。
- C、Python、C++、Java、PHP、Go等编程语言都支持 JSON。
JSON的实例如下:
{ "sites": [ { "name":"百度" , "url":"www.baidu.com" }, { "name":"google" , "url":"www.google.com" }, { "name":"微博" , "url":"www.weibo.com" } ]}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
JSON语法:
- 数据在 名称/值 对中
- 数据由逗号 , 分隔
- 使用斜杆来转义 \ 字符
- 大括号 {} 保存对象
{ "name":"baidu", "alexa":10000, "site":null }
- 1
- 中括号 [] 保存数组,数组可以包含多个对象
[ "Google", "baidu", "Taobao" ]
- 1
如果想学习更多关于JSON文件的知识,可以点击学习。
jsoncpp配置
解析JSON文件我们需要网上开源的库,在这里我用的是jsoncpp库。jsoncpp库的配置相比于tinyXML2稍难,不过按照步骤来就好。
- 首先下载jsoncpp,可以点击下载。
- 下载好之后,解压到本地文件,我们解析JSON文件一般只需要文件中的include文件和src文件。如图:
- 创建解析JSON文件的工程,然后把这两个文件复制到工程目录中。下图是复制好的文件:
- 配置项目的依赖库
右击项目 -》属性 -》C\C++ -》常规 -》附加包含目录,添加依赖,就是刚才你放的include文件目录地址,每个人都不一样,如图所示:
这一步就是引用库文件,引用出差错一般会#include标红,注意头文件引用的绝对路径就行。
5. 把src文件中lib_json下的.cpp文件导入目录,如图所示:
这样就配置好了,配置完过后,可能会有头文件引用标红,分别添加相应依赖就可以了。
C++读写JSON
读取JSON文件之前,首先要了解jsoncpp库中的两个类:Value和Reader。
在这里我就不多讲了,可以参考这篇博客:
读取简单字符串
定义简单的字符串保存json数据
{ "name" : "shuiyixin", "age" : "21", "sex" : "man"}
- 1
- 2
- 3
- 4
- 5
读取代码如下:
void readStrJson(){ //字符串 string str = "{\"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["age"].asInt(); string sex = root["sex"].asString(); //输出 cout << name+ "," << age << "," << sex << endl; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
读取复杂字符串
json数据如下:
{ "name":"shuiyixin", "major":[ { "AI":"MachineLearning" }, { "AI":"DeepLearning" }, { "AI":"ComputerVision" }]}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
代码如下:
void readStrProJson(){ string strValue = "{\"name\":\"wangqinghe\",\"major\":[{\"AI\":\"MachineLearning\"},{\"AI\":\"DeepLearning\"},{\"AI\":\"PictureLearning\"}]}"; Json::Reader reader; Json::Value value; if (reader.parse(strValue, value)) { string str = value["name"].asString(); cout << "name:" <<str<< endl; Json::Value arrayobj = value["major"]; for (unsigned int i = 0; i < arrayobj.size(); i++) { str = arrayobj[i]["AI"].asString(); cout << str << endl; } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
读取JSON文件
文件名为demo.json,内容如下:
{ "age" : "22", "bro" : { "friend_age" : "25", "friend_name" : "Wangwenjian", "friend_sex" : "man" }, "hobby" : [ "sing", "run", "basketball" ], "name" : "Wangqinghe", "sex" : "man"}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
读取文件相关的代码如下:
void readFileJson(){ Json::Reader reader; Json::Value root; ifstream in("demo.json", ios::binary); if (!in.is_open()) { cout << "Error opening file\"; return ; } if (reader.parse(in, root)) { string name = root["name"].asString(); string age = root["age"].asString(); string sex = root["sex"].asString(); cout << "name:" << name << endl; cout << "age:" << age << endl; cout << "sex:" << sex << endl; string friend_name = root["bro"]["friend_name"].asString(); string friend_age = root["bro"]["friend_age"].asString(); string friend_sex = root["bro"]["friend_sex"].asString(); cout << "friend_name:" << friend_name << endl; cout << "friend_age:" << friend_age << endl; cout << "friend_sex:" << friend_sex << endl; for (unsigned int i = 0; i < root["hobby"].size(); i++) { string str = root["hobby"][i].asString(); cout << str << "\t"; } } else { cout << "parse error\" << endl; } in.close();}
- 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
写入JSON文件
写入的文件就是上面的demo.json文件,代码如下:
void writeFileJson(){ //根节点 Json::Value root; root["name"] = Json::Value("Wangqinghe"); root["age"] = Json::Value("22"); root["sex"] = Json::Value("man"); //子节点 Json::Value bro; bro["friend_name"] = Json::Value("Wangwenjian"); bro["friend_age"] = Json::Value("25"); bro["friend_sex"] = Json::Value("man"); root["bro"] = Json::Value(bro); //数组形式 root["hobby"].append("sing"); root["hobby"].append("run"); root["hobby"].append("basketball"); cout << "StyledWriter:" << endl; Json::StyledWriter sw; cout << sw.write(root) << endl << endl; ofstream os; os.open("demo.json", std::ios::out); if (!os.is_open()) { cout << "error:can't find the file" << endl; } os << sw.write(root); os.close();}
- 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
本篇参考了
代码下载: