C++ JSON处理库 CJsonObject 的使用


一、简介

CJsonObject 是一个基于cJSON开发的、适用于c++的轻量级的JSON处理器。
开源地址:
​​ ​ https://gitee.com/Bwar/CJsonObject.git​

二、使用示例

1. 解析一个json格式文件

2.1.1 a.json

{"a":"hello"}

2.1.2 解析程序

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include "../CJsonObject.hpp"

using namespace std;
int main(int argc, char* argv[])
{
std::ifstream fin(argv[1]);
std::string strValue;
if (fin.good())
{
neb::CJsonObject oJson;
std::stringstream ssContent;
ssContent << fin.rdbuf();
if (oJson.Parse(ssContent.str()))
{
std::cout << oJson.ToString() << std::endl;
oJson.Get("a", strValue);
std::cout << "a=" << strValue << std::endl;
}
else
{
std::cerr << "parse json error" << "\n";// << ssContent.str() << std::endl;
}
fin.close();
}
}

2.1.3 运行结果

./CJsonObjectTest "a.js" 
{"a":"12345"}
a=12345

2. 解析一个字符串并读取键值

2.2.1 代码

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include "../CJsonObject.hpp"

using namespace std;

int main(int argc, char* argv[])
{
std::string strValue;

int iValue;
double fTimeout;

neb::CJsonObject oJson("{\"refresh_interval\":60,"
"\"test_float\":[18.0, 10.0, 5.0],"
"\"test_int\":[135355, -1844674407370955161, -935375],"
"\"timeout\":12.5,"
"\"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\":[]"
"}"
"]"
"}");
cout << oJson.ToString() << endl;
cout << "-------------------------------------------------------------------" << endl;
cout << oJson["dynamic_loading"][0]["cmd"][1]("class") << endl;
}

2.2.2 输出

./CJsonObjectTest 
{"refresh_interval":60,"test_float":[18.000000000000000,10.000000000000000,5.000000000000000],"test_int":[135355,-1844674407370955161,-935375],"timeout":12.500000000000000,"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":[]}]}
-------------------------------------------------------------------
neb::CmdUserLogout

3. 取值及修改值

int iValue;
double fTimeout;

oJson["dynamic_loading"][0]["cmd"][0].Get("cmd", iValue);
cout << "iValue = " << iValue << endl;
oJson["dynamic_loading"][0]["cmd"][0].Replace("cmd", -2001);
oJson["dynamic_loading"][0]["cmd"][0].Get("cmd", iValue);
cout << "iValue = " << iValue << endl;

oJson.Get("timeout", fTimeout);

输出:

./CJsonObjectTest 
iValue = 2001
iValue = -2001

4. 增加键

2.4.1 代码

neb::CJsonObject oJson("{\"refresh_interval\":60,"
"\"test_float\":[18.0, 10.0, 5.0],"
"\"timeout\":12.5"
"}");


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");
cout << oJson.ToFormattedString() << endl;

2.4.2 输出

./CJsonObjectTest 
{
"refresh_interval": 60,
"test_float": [18.000000000000000, 10.000000000000000, 5.000000000000000],
"timeout": 12.500000000000000,
"depend": {
"nebula": "https://github.com/Bwar/Nebula",
"bootstrap": ["BEACON", "LOGIC", "LOGGER", "INTERFACE", "ACCESS"]
}
}

5. 删除键

oCopyJson["depend"]["bootstrap"].Delete(1);

6. 复制对象

neb::CJsonObject oCopyJson = oJson;
if (oCopyJson == oJson)
{
cout << "json equal" << endl;
}

输出 :

json equal

7. 遍历键值

2.7.1 代码

neb::CJsonObject oJson("{\"refresh_interval\":60,"
"\"test_float\":[18.0, 10.0, 5.0],"
"\"test_int\":[135355, -1844674407370955161, -935375],"
"\"timeout\":12.5,"
"\"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\":[]"
"}"
"]"
"}");


string strTraversing;
while(oJson["dynamic_loading"][0].GetKey(strTraversing))
{
cout << "traversing: " << strTraversing << endl;
}

2.7.2 输出

./CJsonObjectTest 
traversing: so_path
traversing: load
traversing: version
traversing: cmd
traversing: module

8. 增加键

oJson["dynamic_loading"][0].Add("new_key", "new_value");
while(oJson["dynamic_loading"][0].GetKey(strTraversing))
{
cout << "traversing: " << strTraversing << endl;
}

9. Get方法数组值

2.9.1 代码

neb::CJsonObject oJson("{\"refresh_interval\":60,"
"\"test_float\":[18.0, 10.0, 5.0],"
"\"test_int\":[135355, -1844674407370955161, -935375],"
"\"timeout\":12.5"
"}");


cout << oJson["test_float"].GetArraySize() << endl;
float fTestValue = 0.0;
for (int i = 0; i < oJson["test_float"].GetArraySize(); ++i)
{
oJson["test_float"].Get(i, fTestValue);
cout << fTestValue << "\t in string: " << oJson["test_float"](i) << endl;
}

2.9.2 输出

./CJsonObjectTest 
3
18 in string: 18.000000
10 in string: 10.000000
5 in string: 5.000000

10. 函数下标取数组值

2.10.1 代码

neb::CJsonObject oJson("{\"refresh_interval\":60,"
"\"test_float\":[18.0, 10.0, 5.0],"
"\"test_int\":[135355, -1844674407370955161, -935375],"
"\"timeout\":12.5"
"}");



for (int i = 0; i < oJson["test_int"].GetArraySize(); ++i)
{
cout << "in string: " << oJson["test_int"](i) << endl;
}

2.10.2 输出

./CJsonObjectTest 
in string: 135355
in string: -1844674407370955161
in string: -935375

11. 空值作为键和值

2.11.1 代码

neb::CJsonObject oJson("{\"refresh_interval\":60,"
"\"test_float\":[18.0, 10.0, 5.0],"
"\"test_int\":[135355, -1844674407370955161, -935375],"
"\"timeout\":12.5"
"}");



oJson.AddNull("null_value");
cout << oJson.IsNull("test_float") << "\t" << oJson.IsNull("null_value") << endl;
oJson["test_float"].AddNull();
cout << oJson.ToString() << endl;

2.11.2 输出

./CJsonObjectTest 
0 1
{"refresh_interval":60,"test_float":[18.000000000000000,10.000000000000000,5.000000000000000,null],"test_int":[135355,-1844674407370955161,-935375],"timeout":12.500000000000000,"null_value":null}

12. 判断键是否存在

if (oJson.KeyExist("timeout"))
cout << "timeout key exist" << endl;