经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
C++ Json解析CJsonObject的详细使用
来源:cnblogs  作者:C#爆破  时间:2019/4/23 8:54:37  对本文有异议

引用头文件:

  1. #include <string>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cassert>
  5. #include <stdio.h>
  6. #include <io.h>
  7. #include<sstream>
  8. #include <vector>
  9. #include "CJsonObject.hpp"
  10. using namespace std;
  11. using namespace neb;
  12. CJsonObject Objson;

 

  1. 1 __declspec(dllexport) void __stdcall read_JSon(char* path)
  2. 2 {
  3. 3 ifstream t(path);
  4. 4 string str((istreambuf_iterator<char>(t)),istreambuf_iterator<char>());
  5. 5 string strValue;
  6. 6 Objson= CJsonObject(str);
  7. 7 cout << "-------------------------------------------------------------------" << std::endl;}
加载JSON文件
  1. 1 //保存JSON文件
  2. 2 __declspec(dllexport) void __stdcall write_string_to_file_append(char* file_string)
  3. 3 {
  4. 4 /*remove(file_string);*/
  5. 5 std::ofstream OsWrite(file_string, std::ofstream::trunc);
  6. 6 OsWrite << Objson.ToString();
  7. 7 OsWrite << std::endl;
  8. 8 OsWrite.close();
  9. 9 std::cout << "------------------执行完毕------------------------" << std::endl;
  10. 10 }
保存修改后的JSON结构

1.修改如下图数据结构:

  1. 1 __declspec(dllexport) void __stdcall FxmWindDirectBaseLine(int num, int count, char** pArr, int* sArr, int* iArr)
  2. 2 {
  3. 3
  4. 4 string iValue;
  5. 5 for (int i = 0; i < Objson["WindDirectLine"]["coordinates"][num]["mWindDirectBaseLine"].GetArraySize(); i++)
  6. 6 {
  7. 7 Objson["WindDirectLine"]["coordinates"][num]["mWindDirectBaseLine"].Delete(i);
  8. 8 i--;
  9. 9 }
  10. 10 cout << "---------------" << endl;
  11. 11 cout << Objson["WindDirectLine"]["coordinates"][num]["mWindDirectBaseLine"].GetArraySize() << endl;
  12. 12 CJsonObject je;
  13. 13 for (int i = 0; i < count; i++)
  14. 14 {
  15. 15 iValue = pArr[i];
  16. 16 je.Add("p", iValue);
  17. 17 je.Add("s", sArr[i]);
  18. 18 je.Add("i", iArr[i]);
  19. 19 std::cout << "pArr:" << je.ToString() << std::endl;
  20. 20 Objson["WindDirectLine"]["coordinates"][num]["mWindDirectBaseLine"].Add(je);
  21. 21 je.Clear();
  22. 22 }
  23. 23 cout <<"---------------" << endl;
  24. 24 for (int i = 0; i < Objson["WindDirectLine"]["coordinates"][num]["mWindDirectBaseLine"].GetArraySize(); i++)
  25. 25 {
  26. 26 cout << "mWindDirectBaseLine" << i << ":" << Objson["WindDirectLine"]["coordinates"][num]["mWindDirectBaseLine"][i]("p") << endl;
  27. 27 }
  28. 28 cout << "---------------" << endl;
  29. 29 }
修改mWindDirectBaseLine节点

2.修改如下图数据结构:

  1. 1 __declspec(dllexport) void __stdcall UpmWindDirectBars(int num,int count, char** fpArr,int* fsArr, int* fiArr, char** hpArr, int* hsArr, int* hiArr)
  2. 2 {
  3. 3 string iValue;
  4. 4 for (int i = 0; i < Objson["WindDirectLine"]["coordinates"][num]["mWindDirectBars"].GetArraySize(); i++)
  5. 5 {
  6. 6 Objson["WindDirectLine"]["coordinates"][num]["mWindDirectBars"].Delete(i);
  7. 7 i--;
  8. 8 }
  9. 9 cout << "---------------" << endl;
  10. 10 CJsonObject je;
  11. 11 for (int i = 0; i < count; i++)
  12. 12 {
  13. 13 iValue = fpArr[i];
  14. 14 je.AddEmptySubObject("f");
  15. 15 je["f"].Add("p", iValue);
  16. 16 je["f"].Add("s", fsArr[i]);
  17. 17 je["f"].Add("i", fiArr[i]);
  18. 18 iValue = hpArr[i];
  19. 19 je.AddEmptySubObject("h");
  20. 20 je["h"].Add("p", iValue);
  21. 21 je["h"].Add("s", hsArr[i]);
  22. 22 je["h"].Add("i", hiArr[i]);
  23. 23 std::cout << "pArr:" << je.ToString() << std::endl;
  24. 24 Objson["WindDirectLine"]["coordinates"][num]["mWindDirectBars"].Add(je);
  25. 25 je.Clear();
  26. 26 }
  27. 27 cout << "---------------" << endl;
  28. 28 }
修改mWindDirectBars节点

3.修改如下图数据结构:

  1. 1 __declspec(dllexport) void UpHuxianArrays( int count, double* fpArr, double* fsArr, double* fiArr)
  2. 2 {
  3. 3 std::cout << "-------------------------------------------------------------------" << std::endl;
  4. 4
  5. 5
  6. 6 for (int i = 0; i <Objson["resultWindSpeedGrid"]["HuxianArrays"].GetArraySize(); i++)
  7. 7 {
  8. 8 Objson["resultWindSpeedGrid"]["HuxianArrays"].Delete(i);
  9. 9 i--;
  10. 10 }
  11. 11 cout << "Count:" << Objson["resultWindSpeedGrid"]["HuxianArrays"].GetArraySize() << endl;
  12. 12 cout << "---------------" << endl;
  13. 13 CJsonObject je;
  14. 14 for (int i = 0; i < count; i++)
  15. 15 {
  16. 16 je.AddEmptySubArray("Params");
  17. 17 je["Params"].Add(fpArr[i]);
  18. 18 je["Params"].Add(fsArr[i]);
  19. 19 je["Params"].Add(fiArr[i]);
  20. 20 std::cout << "pArr:" << je.ToString() << std::endl;
  21. 21 Objson["resultWindSpeedGrid"]["HuxianArrays"].Add(je);
  22. 22 je.Clear();
  23. 23 }
  24. 24 Objson["resultWindSpeedGrid"].Replace("HuxianShu", Objson["resultWindSpeedGrid"]["HuxianArrays"].GetArraySize());
  25. 25 cout << "---------------" << endl;
  26. 26 }
修改HuxianArrays节点


官方案例:

  1. 1 int iValue;
  2. 2 std::string strValue;
  3. 3 neb::CJsonObject oJson("{\"refresh_interval\":60,"
  4. 4 "\"dynamic_loading\":["
  5. 5 "{"
  6. 6 "\"so_path\":\"plugins/User.so\", \"load\":false, \"version\":1,"
  7. 7 "\"cmd\":["
  8. 8 "{\"cmd\":2001, \"class\":\"neb::CmdUserLogin\"},"
  9. 9 "{\"cmd\":2003, \"class\":\"neb::CmdUserLogout\"}"
  10. 10 "],"
  11. 11 "\"module\":["
  12. 12 "{\"path\":\"im/user/login\", \"class\":\"neb::ModuleLogin\"},"
  13. 13 "{\"path\":\"im/user/logout\", \"class\":\"neb::ModuleLogout\"}"
  14. 14 "]"
  15. 15 "},"
  16. 16 "{"
  17. 17 "\"so_path\":\"plugins/ChatMsg.so\", \"load\":false, \"version\":1,"
  18. 18 "\"cmd\":["
  19. 19 "{\"cmd\":2001, \"class\":\"neb::CmdChat\"}"
  20. 20 "],"
  21. 21 "\"module\":[]"
  22. 22 "}"
  23. 23 "]"
  24. 24 "}");
  25. 25 std::cout << oJson.ToString() << std::endl;
  26. 26 std::cout << "-------------------------------------------------------------------" << std::endl;
  27. 27 std::cout << oJson["dynamic_loading"][0]["cmd"][1]("class") << std::endl;
  28. 28 oJson["dynamic_loading"][0]["cmd"][0].Get("cmd", iValue);
  29. 29 std::cout << "iValue = " << iValue << std::endl;
  30. 30 oJson["dynamic_loading"][0]["module"][0].Get("path", strValue);
  31. 31 std::cout << "strValue = " << strValue << std::endl;
  32. 32 std::cout << "-------------------------------------------------------------------" << std::endl;
  33. 33 oJson.AddEmptySubObject("depend");
  34. 34 oJson["depend"].Add("nebula", "https://github.com/Bwar/Nebula");
  35. 35 oJson["depend"].AddEmptySubArray("bootstrap");
  36. 36 oJson["depend"]["bootstrap"].Add("BEACON");
  37. 37 oJson["depend"]["bootstrap"].Add("LOGIC");
  38. 38 oJson["depend"]["bootstrap"].Add("LOGGER");
  39. 39 oJson["depend"]["bootstrap"].Add("INTERFACE");
  40. 40 oJson["depend"]["bootstrap"].Add("ACCESS");
  41. 41 oJson["depend"].AddEmptySubArray("boot");
  42. 42 CJsonObject ject;
  43. 43 string iValues;
  44. 44 for (int i = 0; i < count; i++)
  45. 45 {
  46. 46 iValues = pArr[i];
  47. 47 ject.Add("p", iValues);
  48. 48 ject.Add("s", 0);
  49. 49 ject.Add("i", -1);
  50. 50 std::cout << "pArr:" << ject.ToString() << std::endl;
  51. 51 oJson["depend"]["boot"].Add(ject);
  52. 52 ject.Clear();
  53. 53 }
  54. 54 std::cout << oJson.ToString() << std::endl;
  55. 55 std::cout << "-------------------------------------------------------------------" << std::endl;
  56. 56 std::cout << oJson.ToFormattedString() << std::endl;
官方代码


完整案例下载地址:https://files-cdn.cnblogs.com/files/motao9527/CJsonObject.rar

原文链接:http://www.cnblogs.com/motao9527/p/10749039.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号