C++ Json解析CJsonObject的详细使用

2019-04-25 06:49:28来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

引用头文件:

#include <string>
#include <iostream>
#include <fstream>
#include <cassert>
#include <stdio.h>
#include <io.h>
#include<sstream>
#include <vector>
#include "CJsonObject.hpp"

    using namespace std;
    using namespace neb;
  	CJsonObject Objson;

 

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

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

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

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

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

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

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


官方案例:

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


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


原文链接:https://www.cnblogs.com/motao9527/p/10749039.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:短语

下一篇:C++中typedef enum 和 enum