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

引用头文件:

#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;
 __declspec(dllexport)  void  __stdcall read_JSon(char* path)
{
ifstream t(path);
string str((istreambuf_iterator<char>(t)),istreambuf_iterator<char>());
string strValue;
Objson= CJsonObject(str);
cout << "-------------------------------------------------------------------" << std::endl;}

加载JSON文件

     //保存JSON文件
__declspec(dllexport) void __stdcall write_string_to_file_append(char* file_string)
{
/*remove(file_string);*/
std::ofstream OsWrite(file_string, std::ofstream::trunc);
OsWrite << Objson.ToString();
OsWrite << std::endl;
OsWrite.close();
std::cout << "------------------执行完毕------------------------" << std::endl;
}

保存修改后的JSON结构

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

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

 __declspec(dllexport)  void  __stdcall FxmWindDirectBaseLine(int num,  int count, char** pArr, int* sArr, int* iArr)
{ string iValue;
for (int i = ; i < Objson["WindDirectLine"]["coordinates"][num]["mWindDirectBaseLine"].GetArraySize(); i++)
{
Objson["WindDirectLine"]["coordinates"][num]["mWindDirectBaseLine"].Delete(i);
i--;
}
cout << "---------------" << endl;
cout << Objson["WindDirectLine"]["coordinates"][num]["mWindDirectBaseLine"].GetArraySize() << endl;
CJsonObject je;
for (int i = ; i < count; i++)
{
iValue = pArr[i];
je.Add("p", iValue);
je.Add("s", sArr[i]);
je.Add("i", iArr[i]);
std::cout << "pArr:" << je.ToString() << std::endl;
Objson["WindDirectLine"]["coordinates"][num]["mWindDirectBaseLine"].Add(je);
je.Clear();
}
cout <<"---------------" << endl;
for (int i = ; i < Objson["WindDirectLine"]["coordinates"][num]["mWindDirectBaseLine"].GetArraySize(); i++)
{
cout << "mWindDirectBaseLine" << i << ":" << Objson["WindDirectLine"]["coordinates"][num]["mWindDirectBaseLine"][i]("p") << endl;
}
cout << "---------------" << endl;
}

修改mWindDirectBaseLine节点

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

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

 __declspec(dllexport)  void  __stdcall UpmWindDirectBars(int num,int count, char** fpArr,int* fsArr, int* fiArr, char** hpArr, int* hsArr, int* hiArr)
{
string iValue;
for (int i = ; i < Objson["WindDirectLine"]["coordinates"][num]["mWindDirectBars"].GetArraySize(); i++)
{
Objson["WindDirectLine"]["coordinates"][num]["mWindDirectBars"].Delete(i);
i--;
}
cout << "---------------" << endl;
CJsonObject je;
for (int i = ; i < count; i++)
{
iValue = fpArr[i];
je.AddEmptySubObject("f");
je["f"].Add("p", iValue);
je["f"].Add("s", fsArr[i]);
je["f"].Add("i", fiArr[i]);
iValue = hpArr[i];
je.AddEmptySubObject("h");
je["h"].Add("p", iValue);
je["h"].Add("s", hsArr[i]);
je["h"].Add("i", hiArr[i]);
std::cout << "pArr:" << je.ToString() << std::endl;
Objson["WindDirectLine"]["coordinates"][num]["mWindDirectBars"].Add(je);
je.Clear();
}
cout << "---------------" << endl;
}

修改mWindDirectBars节点

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

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

 __declspec(dllexport)  void  UpHuxianArrays( int count, double* fpArr, double* fsArr, double* fiArr)
{
std::cout << "-------------------------------------------------------------------" << std::endl; for (int i = ; i <Objson["resultWindSpeedGrid"]["HuxianArrays"].GetArraySize(); i++)
{
Objson["resultWindSpeedGrid"]["HuxianArrays"].Delete(i);
i--;
}
cout << "Count:" << Objson["resultWindSpeedGrid"]["HuxianArrays"].GetArraySize() << endl;
cout << "---------------" << endl;
CJsonObject je;
for (int i = ; i < count; i++)
{
je.AddEmptySubArray("Params");
je["Params"].Add(fpArr[i]);
je["Params"].Add(fsArr[i]);
je["Params"].Add(fiArr[i]);
std::cout << "pArr:" << je.ToString() << std::endl;
Objson["resultWindSpeedGrid"]["HuxianArrays"].Add(je);
je.Clear();
}
Objson["resultWindSpeedGrid"].Replace("HuxianShu", Objson["resultWindSpeedGrid"]["HuxianArrays"].GetArraySize());
cout << "---------------" << endl;
}

修改HuxianArrays节点

官方案例:

 int iValue;
std::string strValue;
neb::CJsonObject oJson("{\"refresh_interval\":60,"
"\"dynamic_loading\":["
"{"
"\"so_path\":\"plugins/User.so\", \"load\":false, \"version\":1,"
"\"cmd\":["
"{\"cmd\":2001, \"class\":\"neb::CmdUserLogin\"},"
"{\"cmd\":2003, \"class\":\"neb::CmdUserLogout\"}"
"],"
"\"module\":["
"{\"path\":\"im/user/login\", \"class\":\"neb::ModuleLogin\"},"
"{\"path\":\"im/user/logout\", \"class\":\"neb::ModuleLogout\"}"
"]"
"},"
"{"
"\"so_path\":\"plugins/ChatMsg.so\", \"load\":false, \"version\":1,"
"\"cmd\":["
"{\"cmd\":2001, \"class\":\"neb::CmdChat\"}"
"],"
"\"module\":[]"
"}"
"]"
"}");
std::cout << oJson.ToString() << std::endl;
std::cout << "-------------------------------------------------------------------" << std::endl;
std::cout << oJson["dynamic_loading"][]["cmd"][]("class") << std::endl;
oJson["dynamic_loading"][]["cmd"][].Get("cmd", iValue);
std::cout << "iValue = " << iValue << std::endl;
oJson["dynamic_loading"][]["module"][].Get("path", strValue);
std::cout << "strValue = " << strValue << std::endl;
std::cout << "-------------------------------------------------------------------" << std::endl;
oJson.AddEmptySubObject("depend");
oJson["depend"].Add("nebula", "https://github.com/Bwar/Nebula");
oJson["depend"].AddEmptySubArray("bootstrap");
oJson["depend"]["bootstrap"].Add("BEACON");
oJson["depend"]["bootstrap"].Add("LOGIC");
oJson["depend"]["bootstrap"].Add("LOGGER");
oJson["depend"]["bootstrap"].Add("INTERFACE");
oJson["depend"]["bootstrap"].Add("ACCESS");
oJson["depend"].AddEmptySubArray("boot");
CJsonObject ject;
string iValues;
for (int i = ; i < count; i++)
{
iValues = pArr[i];
ject.Add("p", iValues);
ject.Add("s", );
ject.Add("i", -);
std::cout << "pArr:" << ject.ToString() << std::endl;
oJson["depend"]["boot"].Add(ject);
ject.Clear();
}
std::cout << oJson.ToString() << std::endl;
std::cout << "-------------------------------------------------------------------" << std::endl;
std::cout << oJson.ToFormattedString() << std::endl;

官方代码

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

上一篇:Web大文件(夹)上传(断点续传)控件发布-Xproer.HttpUploader6


下一篇:setinIerval和setTimeout的区别?