赞
踩
为什么要用json文件呢?
我们最常使用的存储数据的方式有很多,比如利用txt文件存,利用xml存,利用word存,利用Excel存,如果我们要求比较高,还可以使用数据库存。
相对于txt,word来说,json格式更加明确,获取重要信息非常方便。
相对于xml来说,json格式更加简洁,存储同样的文件,花费的内存更小。
相对于Excel来说,json更适合存储字符类文件。Excel相当于比较简单的数据库了。
相对于数据库来说,json更加方便,数据库我们还需要做一些设置,安装一些软件。json可以直接使用。
JSON 比更小、更快,更易解析。JSON 易于人阅读和编写。C、Python、C++、Java、PHP、Go等编程语言都支持 JSON。
- {
- "sites": [
- { "name":"百度" , "url":"www.baidu.com" },
- { "name":"google" , "url":"www.google.com" },
- { "name":"微博" , "url":"www.weibo.com" }
- ]
- }
JSON语法:
解析JSON文件我们需要网上开源的库,在这里我用的是jsoncpp库。jsoncpp库的配置相比于tinyXML2稍难,不过按照步骤来就好。
1、首先下载jsoncpp,可以点击jsoncpp下载。
2、下载好之后,解压到本地文件,我们解析JSON文件一般只需要文件中的include文件和src文件。
3、创建解析JSON文件的工程,然后把这两个文件复制到工程目录中
4、配置项目的依赖库
右击项目 -》属性 -》C\C++ -》常规 -》附加包含目录,添加依赖,就是刚才你放的include文件目录地址,每个人都不一样,如图所示:
这一步就是引用库文件,引用出差错一般会#include标红,注意头文件引用的绝对路径就行。
5. 把src文件中lib_json下的.cpp文件导入目录,如图所示:
读取JSON文件之前,首先要了解jsoncpp库中的两个类:Value和Reader。
在这里我就不多讲了,可以参考这篇博客:
Value、Reader
json数据如下
- {
- "name" : "shuiyixin",
- "age" : "21",
- "sex" : "man"
- }
读取代码如下:
- 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;
- }
-
- }
- {
- "A": {
- "type": "SINT",
- "value": 0
- },
- "age": "18",
- "B": {
- "type": "INT",
- "value": 0
- },
- "C": {
- "type": "DATE",
- "value": {
- "year": [ "2019", "2013", "2012" ],
- "month": [ "1", "2", "3" ]
-
- }
- },
- "friend": {
- "friend_age": "18",
- "friend_name": "qiwen",
- "friend_sex": "man"
- },
- "hobby": [ "bamintan", "eat", "rap" ],
- "name": "hemenglin",
- "sex": "male"
-
- }
读取文件相关的代码如下:
- void readFileJson()
- {
- Json::Reader reader;
- Json::Value root;
-
- ifstream in("demo.json", ios::binary);
- if (!in.is_open())
- {
- cout << "Error opening file\n";
- 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["friend"]["friend_name"].asString();
- string friend_age = root["friend"]["friend_age"].asString();
- string friend_sex = root["friend"]["friend_sex"].asString();
-
- string atype = root["A"]["type"].asString();
- string avalue = root["A"]["value"].asString();
-
- string btype = root["B"]["type"].asString();
- string bvalue = root["B"]["value"].asString();
-
- cout << "atype:" << atype << endl;
- cout << "avalue:" << avalue << endl;
-
- cout << "btype:" << btype << endl;
- cout << "bvalue:" << bvalue << endl;
-
- cout << "friend_name:" << friend_name << endl;
- cout << "friend_age:" << friend_age << endl;
- cout << "friend_sex:" << friend_sex << endl;
-
- cout << "hobby:" << endl;
- for (unsigned int i = 0; i < root["hobby"].size(); i++)
- {
- string str = root["hobby"][i].asString();
- cout << str << "\t";
- }
-
- string Ctype = root["C"]["type"].asString();
- cout << "Ctype:" << Ctype << endl;
- //string Cvalue = root["C"]["value"]["year"].asString();
- //cout << "Cvalue:" << Cvalue << endl;
-
- cout << "DATE OF YEAR:" << endl;
- for (unsigned int i = 0; i < root["C"]["value"]["year"].size(); i++)
- {
- string str = root["C"]["value"]["year"][i].asString();
- cout << str << "\t";
- }
-
-
-
-
- }
- else
- {
- cout << "parse error\n" << endl;
- }
- in.close();
-
- }
读的效果如图:
写入的文件就是上面的demo.json文件,代码如下:
- void writeFileJson()
- {
- //根节点
- Json::Value root;
- root["name"] = Json::Value("hemenglin");
- root["age"] = Json::Value("18");
- root["sex"] = Json::Value("male");
-
- //子节点
- Json::Value bro;
- bro["friend_name"] = Json::Value("qiwen");
- bro["friend_age"] = Json::Value("18");
- bro["friend_sex"] = Json::Value("man");
-
- root["friend"] = Json::Value(bro);
-
-
- //ctest
- Json::Value Ctest;
- Ctest["type"] = Json::Value("DATE");
- Ctest["value"] = Json::Value();
- root["C"] = Json::Value(Ctest);
-
- Json::Value Ctest2;
- Ctest2["year"] = Json::Value();
- Ctest2["month"] = Json::Value();
- Ctest2["day"] = Json::Value();
- root["C"]["value"] = Json::Value(Ctest2);
-
-
-
- root["C"]["value"]["year"].append("2019");
- root["C"]["value"]["year"].append("2013");
- root["C"]["value"]["year"].append("2012");
-
- root["C"]["value"]["month"].append("3");
- root["C"]["value"]["month"].append("2");
- root["C"]["value"]["month"].append("1");
- //ctest
-
- //数组形式
- root["hobby"].append("bamintan");
- root["hobby"].append("eat");
- root["hobby"].append("rap");
-
-
- cout << "\nStyledWriter:" << 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.如果文件存在,写入过程不会覆盖文件中原有数据,而是将新数据写在原有数据后面。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。