CJsonObject 是一个基于cJSON开发的、适用于c++的轻量级的JSON处理器。
开源地址:
https://gitee.com/Bwar/CJsonObject.git
{"a":"hello"}
#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();
}
}
./CJsonObjectTest "a.js"
{"a":"12345"}
a=12345
#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;
}
./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
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
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;
./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"]
}
}
oCopyJson["depend"]["bootstrap"].Delete(1);
neb::CJsonObject oCopyJson = oJson;
if (oCopyJson == oJson)
{
cout << "json equal" << endl;
}
输出 :
json equal
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;
}
./CJsonObjectTest
traversing: so_path
traversing: load
traversing: version
traversing: cmd
traversing: module
oJson["dynamic_loading"][0].Add("new_key", "new_value");
while(oJson["dynamic_loading"][0].GetKey(strTraversing))
{
cout << "traversing: " << strTraversing << endl;
}
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;
}
./CJsonObjectTest
3
18 in string: 18.000000
10 in string: 10.000000
5 in string: 5.000000
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;
}
./CJsonObjectTest
in string: 135355
in string: -1844674407370955161
in string: -935375
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;
./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}
if (oJson.KeyExist("timeout"))
cout << "timeout key exist" << endl;