当前位置:   article > 正文

C++ 操作 (读写)json 文件及jsoncpp的配置

jsoncpp

一、json文件简介


为什么要用json文件呢?

我们最常使用的存储数据的方式有很多,比如利用txt文件存,利用xml存,利用word存,利用Excel存,如果我们要求比较高,还可以使用数据库存。

相对于txt,word来说,json格式更加明确,获取重要信息非常方便。

相对于xml来说,json格式更加简洁,存储同样的文件,花费的内存更小。

相对于Excel来说,json更适合存储字符类文件。Excel相当于比较简单的数据库了。

相对于数据库来说,json更加方便,数据库我们还需要做一些设置,安装一些软件。json可以直接使用。

JSON 比更小、更快,更易解析。JSON 易于人阅读和编写。C、Python、C++、Java、PHP、Go等编程语言都支持 JSON。

二、JSON的实例如下:

  1. {
  2. "sites": [
  3. { "name":"百度" , "url":"www.baidu.com" },
  4. { "name":"google" , "url":"www.google.com" },
  5. { "name":"微博" , "url":"www.weibo.com" }
  6. ]
  7. }

JSON语法:

  • 数据在 名称/值 对中
  • 数据由逗号 , 分隔
  • 使用斜杆来转义 \ 字符
  • 大括号 {} 保存对象

三、jsoncpp配置


解析JSON文件我们需要网上开源的库,在这里我用的是jsoncpp库。jsoncpp库的配置相比于tinyXML2稍难,不过按照步骤来就好。

1、首先下载jsoncpp,可以点击jsoncpp下载。
2、下载好之后,解压到本地文件,我们解析JSON文件一般只需要文件中的include文件和src文件。

3、创建解析JSON文件的工程,然后把这两个文件复制到工程目录中

4、配置项目的依赖库
右击项目 -》属性 -》C\C++ -》常规 -》附加包含目录,添加依赖,就是刚才你放的include文件目录地址,每个人都不一样,如图所示:

 这一步就是引用库文件,引用出差错一般会#include标红,注意头文件引用的绝对路径就行。
5. 把src文件中lib_json下的.cpp文件导入目录,如图所示:

四、C++操作json文件

读取JSON文件之前,首先要了解jsoncpp库中的两个类:Value和Reader。
在这里我就不多讲了,可以参考这篇博客:
Value、Reader

json数据如下

  1. {
  2. "name" : "shuiyixin",
  3. "age" : "21",
  4. "sex" : "man"
  5. }

读取代码如下:

  1. void readStrJson()
  2. {
  3. //字符串
  4. string str =
  5. "{\"name\":\"shuiyixin\",\"age\":21,\"sex\":\"man\"}";
  6. //声明类的对象
  7. Json::Reader reader;
  8. Json::Value root;
  9. //从字符串中读取数据
  10. if (reader.parse(str, root))
  11. {
  12. string name = root["name"].asString();
  13. int age = root["age"].asInt();
  14. string sex = root["sex"].asString();
  15. //输出
  16. cout << name+ "," << age << "," << sex << endl;
  17. }
  18. }

读取JSON文件

  1. {
  2. "A": {
  3. "type": "SINT",
  4. "value": 0
  5. },
  6. "age": "18",
  7. "B": {
  8. "type": "INT",
  9. "value": 0
  10. },
  11. "C": {
  12. "type": "DATE",
  13. "value": {
  14. "year": [ "2019", "2013", "2012" ],
  15. "month": [ "1", "2", "3" ]
  16. }
  17. },
  18. "friend": {
  19. "friend_age": "18",
  20. "friend_name": "qiwen",
  21. "friend_sex": "man"
  22. },
  23. "hobby": [ "bamintan", "eat", "rap" ],
  24. "name": "hemenglin",
  25. "sex": "male"
  26. }

读取文件相关的代码如下: 

  1. void readFileJson()
  2. {
  3. Json::Reader reader;
  4. Json::Value root;
  5. ifstream in("demo.json", ios::binary);
  6. if (!in.is_open())
  7. {
  8. cout << "Error opening file\n";
  9. return ;
  10. }
  11. if (reader.parse(in, root))
  12. {
  13. string name = root["name"].asString();
  14. string age = root["age"].asString();
  15. string sex = root["sex"].asString();
  16. cout << "name:" << name << endl;
  17. cout << "age:" << age << endl;
  18. cout << "sex:" << sex << endl;
  19. string friend_name = root["friend"]["friend_name"].asString();
  20. string friend_age = root["friend"]["friend_age"].asString();
  21. string friend_sex = root["friend"]["friend_sex"].asString();
  22. string atype = root["A"]["type"].asString();
  23. string avalue = root["A"]["value"].asString();
  24. string btype = root["B"]["type"].asString();
  25. string bvalue = root["B"]["value"].asString();
  26. cout << "atype:" << atype << endl;
  27. cout << "avalue:" << avalue << endl;
  28. cout << "btype:" << btype << endl;
  29. cout << "bvalue:" << bvalue << endl;
  30. cout << "friend_name:" << friend_name << endl;
  31. cout << "friend_age:" << friend_age << endl;
  32. cout << "friend_sex:" << friend_sex << endl;
  33. cout << "hobby:" << endl;
  34. for (unsigned int i = 0; i < root["hobby"].size(); i++)
  35. {
  36. string str = root["hobby"][i].asString();
  37. cout << str << "\t";
  38. }
  39. string Ctype = root["C"]["type"].asString();
  40. cout << "Ctype:" << Ctype << endl;
  41. //string Cvalue = root["C"]["value"]["year"].asString();
  42. //cout << "Cvalue:" << Cvalue << endl;
  43. cout << "DATE OF YEAR:" << endl;
  44. for (unsigned int i = 0; i < root["C"]["value"]["year"].size(); i++)
  45. {
  46. string str = root["C"]["value"]["year"][i].asString();
  47. cout << str << "\t";
  48. }
  49. }
  50. else
  51. {
  52. cout << "parse error\n" << endl;
  53. }
  54. in.close();
  55. }

读的效果如图: 

写入JSON文件

写入的文件就是上面的demo.json文件,代码如下:

  1. void writeFileJson()
  2. {
  3. //根节点
  4. Json::Value root;
  5. root["name"] = Json::Value("hemenglin");
  6. root["age"] = Json::Value("18");
  7. root["sex"] = Json::Value("male");
  8. //子节点
  9. Json::Value bro;
  10. bro["friend_name"] = Json::Value("qiwen");
  11. bro["friend_age"] = Json::Value("18");
  12. bro["friend_sex"] = Json::Value("man");
  13. root["friend"] = Json::Value(bro);
  14. //ctest
  15. Json::Value Ctest;
  16. Ctest["type"] = Json::Value("DATE");
  17. Ctest["value"] = Json::Value();
  18. root["C"] = Json::Value(Ctest);
  19. Json::Value Ctest2;
  20. Ctest2["year"] = Json::Value();
  21. Ctest2["month"] = Json::Value();
  22. Ctest2["day"] = Json::Value();
  23. root["C"]["value"] = Json::Value(Ctest2);
  24. root["C"]["value"]["year"].append("2019");
  25. root["C"]["value"]["year"].append("2013");
  26. root["C"]["value"]["year"].append("2012");
  27. root["C"]["value"]["month"].append("3");
  28. root["C"]["value"]["month"].append("2");
  29. root["C"]["value"]["month"].append("1");
  30. //ctest
  31. //数组形式
  32. root["hobby"].append("bamintan");
  33. root["hobby"].append("eat");
  34. root["hobby"].append("rap");
  35. cout << "\nStyledWriter:" << endl;
  36. Json::StyledWriter sw;
  37. cout << sw.write(root) << endl << endl;
  38. ofstream os;
  39. os.open("demo.json", std::ios::out);
  40. if (!os.is_open())
  41. {
  42. cout << "error:can't find the file" << endl;
  43. }
  44. os << sw.write(root);
  45. os.close();
  46. }

写的效果如图:  

要注意的是:

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

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

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/595990
推荐阅读
相关标签
  

闽ICP备14008679号