当前位置:   article > 正文

C++操作json数据_c++ json

c++ json

一、下载jsoncpp

​         首先到如下地址下载jsoncpp源码;https://sourceforge.net/projects/jsoncpp/

        下载完成后解压缩得到如下文件夹:

二、环境配置

​         新建一个测试工程,默认打印helloWorld;

        将jsoncpp-src-0.5.0文件夹复制到工程目录下;

        以源码方式引用jsoncpp,便于管理新建一个json筛选器,并将..\jsoncpp-src-0.5.0\include\json和jsoncpp-src-0.5.0\src\lib_json两个目录里的文件添加到筛选器下:

         右键项目属性,在C/C++/常规/附加包含目录下,添加.\jsoncpp-src-0.5.0\include目录;

         将三个cpp文件的属性设置为不使用预编译头;

三、效果测试

        使用时添加#include "json/json.h"即可,此处以封装json数据为string为例:

四、C++操作json方法       

        首先定义一个json数据格式:

  1. {
  2. "name" : "xiaohuoche"
  3. "age" : 9
  4. "major" : [ "math", "physics"]
  5. “friends” : [
  6. {
  7. “friend_name” : "xiaoshuai"
  8. “friend_age” : 8
  9. "friend_major" : "sing"
  10. },
  11. {
  12. “friend_name” : "xiaomei"
  13. “friend_age” : 6
  14. "friend_major" : "dance"
  15. },
  16. ]
  17. }

4.1 封装json数据为string

  1. std::string WJsonWork::DataToJson()
  2. {
  3. Json::FastWriter writerinfo;
  4. Json::Value writevalueinfo;
  5. writevalueinfo["name"] = "xiaohuoche";
  6. writevalueinfo["age"] = 9;
  7. writevalueinfo["major"].append("math");
  8. writevalueinfo["major"].append("physics");
  9. Json::Value writedata1;
  10. writedata1["friend_name"] = "xiaoshuai";
  11. writedata1["friend_age"] = 8;
  12. writedata1["friend_major"] = "sing";
  13. writevalueinfo["friends"].append(writedata1);
  14. Json::Value writedata2;
  15. writedata2["friend_name"] = "xiaomei";
  16. writedata2["friend_age"] = 6;
  17. writedata2["friend_major"] = "dance";
  18. writevalueinfo["friends"].append(writedata2);
  19. std::string strEmail = writerinfo.write(writevalueinfo);
  20. //缩进输出
  21. Json::StyledWriter sw;
  22. cout << sw.write(writevalueinfo) << endl << endl;
  23. return strEmail;
  24. }

打印结果如下:

4.2 从string中解析json数据

  1. void WJsonWork::ReadJsonStr(string strData)
  2. {
  3. Json::Reader reader;
  4. Json::Value value;
  5. if (reader.parse(strData, value))
  6. {
  7. string name = value["name"].asString();
  8. cout << "name:" << name << endl;
  9. int age = value["age"].asInt();
  10. cout << "age:" << age << endl;
  11. const Json::Value arrayObj1 = value["major"];
  12. for (unsigned int i = 0; i < arrayObj1.size(); i++)
  13. {
  14. string major = arrayObj1[i].asString();
  15. cout << "major:" << major << endl;
  16. }
  17. const Json::Value arrayObj2 = value["friends"];
  18. for (unsigned int i = 0; i < arrayObj2.size(); i++)
  19. {
  20. string friend_name = arrayObj2[i]["friend_name"].asString();
  21. int friend_age = arrayObj2[i]["friend_age"].asInt();
  22. string friend_major = arrayObj2[i]["friend_major"].asString();
  23. cout << "friend_name:" << friend_name <<"\t"
  24. << "friend_age:" << friend_age << "\t"
  25. << "friend_major:" << friend_major << endl;
  26. }
  27. }
  28. }

打印结果如下:

 4.3 写入本地json文件

  1. void WJsonWork::writeJsonFile()
  2. {
  3. //根节点
  4. Json::Value root;
  5. //根节点属性
  6. root["name"] = Json::Value("xiaohuoche");
  7. root["age"] = Json::Value(9);
  8. //根节点属性 :数组形式
  9. root["major"].append("math");
  10. root["major"].append("physics");
  11. //子节点1
  12. Json::Value friends1;
  13. //子节点属性
  14. friends1["friend_name"] = Json::Value("xiaoshuai");
  15. friends1["friend_age"] = Json::Value(8);
  16. friends1["friend_major"] = Json::Value("sing");
  17. //子节点挂到根节点上
  18. root["friends"].append(friends1);
  19. //子节点2
  20. Json::Value friends2;
  21. //子节点属性
  22. friends2["friend_name"] = Json::Value("xiaomei");
  23. friends2["friend_age"] = Json::Value(6);
  24. friends2["friend_major"] = Json::Value("dance");
  25. //子节点挂到根节点上
  26. root["friends"].append(friends2);
  27. //输出到文件
  28. Json::StyledWriter sw;
  29. ofstream os;
  30. os.open("test.json", std::ios::out | std::ios::app);
  31. if (!os.is_open())
  32. cout << "error:can not find or create the file which named \" demo.json\"." << endl;
  33. os << sw.write(root);
  34. os.close();
  35. }

本地结果如下:

4.4 解析本地json文件

  1. void WJsonWork::readJsonFile()
  2. {
  3. Json::Reader reader;
  4. Json::Value root;
  5. //从文件中读取,保证当前文件有demo.json文件
  6. ifstream in("test.json", ios::binary);
  7. if (!in.is_open())
  8. {
  9. cout << "Error opening file\n";
  10. return;
  11. }
  12. if (reader.parse(in, root))
  13. {
  14. //读取根节点信息
  15. string name = root["name"].asString();
  16. cout << "name:" << name << endl;
  17. int age = root["age"].asInt();
  18. cout << "age:" << age << endl;
  19. cout << "major:\t";
  20. //读取根节点数组信息
  21. for (unsigned int i = 0; i < root["major"].size(); i++)
  22. {
  23. string ach = root["major"][i].asString();
  24. cout << ach << '\t';
  25. }
  26. cout << '\n';
  27. //读取子节点数组信息
  28. for (unsigned int i = 0; i < root["friends"].size(); i++)
  29. {
  30. string ach = root["friends"][i]["friend_name"].asString();
  31. cout << "friend_name:" << ach << '\n';
  32. int age = root["friends"][i]["friend_age"].asInt();
  33. cout << "friend_age:" << age << '\n';
  34. ach = root["friends"][i]["friend_major"].asString();
  35. cout << "friend_major:" << ach << '\n';
  36. }
  37. }
  38. else
  39. {
  40. cout << "parse error\n" << endl;
  41. }
  42. in.close();
  43. }

打印结果如下:

五、参考资料

https://blog.csdn.net/u011261430/article/details/77713299?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167365679916800182751024%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167365679916800182751024&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~baidu_landing_v2~default-3-77713299-null-null.142^v71^insert_chatgpt,201^v4^add_ask&utm_term=c%2B%2B%20%20json&spm=1018.2226.3001.4187​​​​​​​

https://blog.csdn.net/shuiyixin/article/details/89330529?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167583625216800184161844%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167583625216800184161844&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-89330529-null-null.142^v73^insert_down1,201^v4^add_ask,239^v1^insert_chatgpt&utm_term=c%2B%2Bjson&spm=1018.2226.3001.4187

https://blog.51cto.com/ives/5522437

https://blog.csdn.net/ouyangfushu/article/details/84993791?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167521636316800184180354%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167521636316800184180354&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~baidu_landing_v2~default-4-84993791-null-null.142^v72^insert_chatgpt,201^v4^add_ask&utm_term=VS%E7%BC%96%E8%AF%91jsoncpp&spm=1018.2226.3001.4187

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

闽ICP备14008679号