GitHub地址:
GitHub - j05070415/RJson: C++ JSON library base on rapidjsonC++ JSON library base on rapidjson. Contribute to j05070415/RJson development by creating an account on GitHub.https://github.com/j05070415/RJson.git
示例代码
JSON创建
RValue o1(alloc);
o1["phone"] = 123455;
o1["name"] = "jone";
o1["addr"] = "xxx@asdfasf";
o1["object"]["name"] = "smith";
o1["object"]["age"] = "13";
o1["array"].append("david");
o1["array"].append(99.1234567);
o1["array"].append(true);
o1["array"].append(-123);
JSON序列化和反序列化
std::string str = "{\"count\":2,\"names\":[\"zhangsan\",wangwu\"]}";
auto doc1 = RDocument::fromJson(str.c_str(), str.size());
auto text = doc1.toJson();
Array类型增删改查
std::string str = "{\"count\":2,\"names\":[{\"name\":\"zhangsan\"},{\"name\":\"wangwu\"}]}";
auto doc = RDocument::fromJson(str.c_str(), str.size());
auto names = doc["names"];
printf("size:%d\n", names.size());
for (unsigned int i=0; i<names.size(); ++i) {
auto name = names[i];
qDebug() << name["name"].toString().c_str();
}
Object类型增删改查
RValue o1(alloc);
o1["phone"] = 123455;
o1["name"] = "jone";
o1["name"] = "jone1";
o1.remove("name");
空对象创建及修改
RValue value(alloc);
value["age"].setObject();
value["times"].setObject();
value["names"].setArray();
value["urls"].setArray();
value["books"].setArray();
基本数值类型
int i1 = 0x7FFFFFFF;//2147483647
unsigned int i2 = 0xFFFFFFFF;//4294967295
long long i3 = 0x7FFFFFFFFFFFFFFF;//9223372036854775807
unsigned long long i4 = 0xFFFFFFFFFFFFFFFF;//18446744073709551615
auto txt1 = QString::fromLocal8Bit("hello workd!你好,世界");
std::string s1(txt1.toUtf8().data());
RValue v0, v1(true), v2(110.123), v3(-100), v4(i1),
v5(i2), v6(i3), v7(i4), v8(s1), v9(s1.c_str()), v10(s1.c_str(), s1.size());
|