c/c++语言开发共享C++ Json解析CJsonObject的详细使用

引用头文件: 1 __declspec(dllexport) void __stdcall read_JSon(char* path) 2 { 3 ifstream t(path); 4 string str((istreambuf_iterator<char>(t)),istreambuf_ite …

引用头文件:

#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;}

 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 }

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

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

 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 }

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

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

 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 }

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

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

 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 }

官方案例:

 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

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/603782.html

(0)
上一篇 2021年5月11日
下一篇 2021年5月12日

精彩推荐