当前位置:   article > 正文

Linux下json文件读写

Linux下json文件读写

参考:C/C++之读写JSON数据_c++读取json文件-CSDN博客

1、安装json库

sudo apt-get install libjsoncpp-dev

2、CMakeLists.txt

  1. cmake_minimum_required(VERSION 2.8)
  2. project(test-json)
  3. add_executable(${PROJECT_NAME} "main.cpp")
  4. target_link_libraries(${PROJECT_NAME} -ljsoncpp)

3、main.cpp

  1. #include <iostream>
  2. #include <fstream>
  3. #include <jsoncpp/json/value.h>
  4. #include <jsoncpp/json/reader.h>
  5. #include <jsoncpp/json/writer.h>
  6. using namespace std;
  7. void readStrProJson();
  8. void readFromFile();
  9. void writeFileJson();
  10. int main()
  11. {
  12. readStrProJson();
  13. readFromFile();
  14. writeFileJson();
  15. return 0;
  16. }
  17. void readStrProJson()
  18. {
  19. string strValue = "{"
  20. "\"name\":\"Derozan\","
  21. "\"major\":["
  22. "{"
  23. "\"Player\":\"EDG player\""
  24. "},"
  25. "{"
  26. "\"Player\":\"Basketball Player\""
  27. "}]"
  28. "}";
  29. Json::Reader reader;
  30. Json::Value value;
  31. if (reader.parse(strValue, value))
  32. {
  33. string out = value["name"].asString();
  34. cout << out << endl;
  35. const Json::Value arrayObj = value["major"];
  36. for (unsigned int i = 0; i < arrayObj.size(); i++)
  37. {
  38. out = arrayObj[i]["Player"].asString();
  39. cout << out<<endl;
  40. }
  41. }
  42. }
  43. void readFromFile() {
  44. Json::Reader reader;
  45. Json::Value root;
  46. const char* path = "./test.json";
  47. std::ifstream infile(path);
  48. if(reader.parse(infile, root)) {
  49. //读取根节点信息
  50. std::string name = root["name"].asString();
  51. int age = root["age"].asInt();
  52. std::string sex = root["sex"].asString();
  53. std::cout << "My name is " << name << std::endl;
  54. std::cout << "I'm " << age << " years old" << std::endl;
  55. std::cout << "I'm a " << sex << std::endl;
  56. //读取子节点信息
  57. std::string friend_name = root["friends"]["friend_name"].asString();
  58. int friend_age = root["friends"]["friend_age"].asInt();
  59. std::string friend_sex = root["friends"]["friend_sex"].asString();
  60. std::cout << "friend's name is " << friend_name << std::endl;
  61. std::cout << "friend's sex is "<<friend_sex << std::endl;
  62. std::cout << "friend is " << friend_age << " years old" << std::endl;
  63. //读取数组信息
  64. std::cout << "my hobby:" << std::endl;
  65. for (unsigned int i = 0; i < root["hobby"].size(); i++)
  66. {
  67. std::string ach = root["hobby"][i].asString();
  68. std::cout << ach << '\t';
  69. }
  70. std::cout << std::endl;
  71. std::cout << "Reading Success!" << std::endl;
  72. }
  73. }
  74. void writeFileJson()
  75. {
  76. //根节点
  77. Json::Value root;
  78. //根节点属性
  79. root["name"] = Json::Value("Derozan");
  80. root["age"] = Json::Value(27);
  81. root["sex"] = Json::Value("man");
  82. //子节点
  83. Json::Value friends;
  84. //子节点属性
  85. friends["friend_name"] = Json::Value("Alen");
  86. friends["friend_age"] = Json::Value(35);
  87. friends["friend_sex"] = Json::Value("man");
  88. //子节点挂到根节点上
  89. root["friends"] = Json::Value(friends);
  90. //数组形式
  91. root["hobby"].append("Basketball");
  92. root["hobby"].append("Swimming");
  93. root["hobby"].append("game");
  94. //直接输出
  95. //cout << "FastWriter:" << endl;
  96. //Json::FastWriter fw;
  97. //cout << fw.write(root) << endl << endl;
  98. //缩进输出
  99. std::cout << "StyledWriter:" << std::endl;
  100. Json::StyledWriter sw;
  101. std::cout << sw.write(root) << std::endl << std::endl;
  102. //输出到文件
  103. std::ofstream os;
  104. const char* path = "./testwrite.json";
  105. os.open(path, std::ios::out | std::ios::app);
  106. if (!os.is_open())
  107. std::cout << "error:can not find or create the file which named \" demo.json\"." << std::endl;
  108. os << sw.write(root);
  109. os.close();
  110. }

4、终端输出

  1. Derozan
  2. EDG player
  3. Basketball Player
  4. My name is Alen
  5. I'm 25 years old
  6. I'm a women
  7. friend's name is Derozan
  8. friend's sex is man
  9. friend is 25 years old
  10. my hobby:
  11. basket women sleep
  12. Reading Success!
  13. StyledWriter:
  14. {
  15. "age" : 27,
  16. "friends" : {
  17. "friend_age" : 35,
  18. "friend_name" : "Alen",
  19. "friend_sex" : "man"
  20. },
  21. "hobby" : [ "Basketball", "Swimming", "game" ],
  22. "name" : "Derozan",
  23. "sex" : "man"
  24. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/291534
推荐阅读
相关标签
  

闽ICP备14008679号