rapidjson::Document document;
document.SetObject();
// 添加name, value
const char* name = "success_url";
const char* value = "https://www.google.com";
document.AddMember(rapidjson::StringRef(name), rapidjson::StringRef(value), document.GetAllocator());
// 添加json object
rapidjson::Value info_objects(rapidjson::kObjectType);
std::string jsonObject = "json_object";
info_objects.AddMember(rapidjson::StringRef("class_room"), rapidjson::StringRef("NO. 6110"), document.GetAllocator());
info_objects.AddMember(rapidjson::StringRef("teacher_name"), rapidjson::StringRef("ZhangSanfeng"), document.GetAllocator());
document.AddMember(rapidjson::StringRef(jsonObject.c_str()), info_objects, document.GetAllocator());
// 添加json array
rapidjson::Value array_objects(rapidjson::kArrayType);
for (int i = 0; i < 2; i++)
{
Value object(kObjectType);
Value nobject(kNumberType);
nobject.SetInt(i);
object.AddMember(StringRef("id"), nobject, document.GetAllocator());
object.AddMember(StringRef("name"), StringRef("zhangsan"), document.GetAllocator());
array_objects.PushBack(object, document.GetAllocator());
}
char * jsonArrayName = "jsonArrayName";
document.AddMember(rapidjson::StringRef(jsonArrayName), array_objects, document.GetAllocator());
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
document.Accept(writer);
std::string json = std::string(buffer.GetString());
LOGD("testAddMember = %s", json.c_str());
rapidjson::StringBuffer s;
rapidjson::Writer<StringBuffer> writer(s);
writer.StartObject(); // Between StartObject()/EndObject(),
writer.Key("hello"); // output a key,
writer.String("world"); // follow by a value.
writer.Key("t");
writer.Bool(true);
writer.Key("f");
writer.Bool(false);
writer.Key("n");
writer.Null();
writer.Key("i");
writer.Uint(123);
writer.Key("pi");
writer.Double(3.1416);
writer.Key("a");
writer.StartArray(); // Between StartArray()/EndArray(),
for (unsigned i = 0; i < 4; i++)
writer.Uint(i); // all values are elements of the array.
writer.EndArray();
writer.EndObject();
std::cout << s.GetString() << std::endl;
std::string json = std::string(buffer.GetString());
假设现有一个json文件。
const char* json = {
"hello": "world",
"t": true ,
"f": false,
"n": null,
"i": 123,
"pi": 3.1416,
"a": [1, 2, 3, 4]
}
#include "rapidjson/document.h"
using namespace rapidjson;
Document document;
document.Parse(json);
const Value& a = document["a"];
assert(a.IsArray());
for (SizeType i = 0; i < a.Size(); i++) // 使用 SizeType 而不是 size_t
printf("a[%d] = %d\n", i, a[i].GetInt());
for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr)
printf("%d ", itr->GetInt());
for (auto& v : a.GetArray())
printf("%d ", v.GetInt());
static const char* kTypeNames[] =
{ "Null", "False", "True", "Object", "Array", "String", "Number" };
for (Value::ConstMemberIterator itr = document.MemberBegin();
itr != document.MemberEnd(); ++itr)
{
printf("Type of member %s is %s\n",
itr->name.GetString(), kTypeNames[itr->value.GetType()]);
}
Value::ConstMemberIterator itr = document.FindMember("hello");
if (itr != document.MemberEnd())
printf("%s\n", itr->value.GetString());
for (auto& m : document.GetObject())
printf("Type of member %s is %s\n",
m.name.GetString(), kTypeNames[m.value.GetType()]);
查询 | 获取 |
---|---|
bool IsNumber() | 不适用 |
bool IsUint() | unsigned GetUint() |
bool IsInt() | int GetInt() |
bool IsUint64() | uint64_t GetUint64() |
bool IsInt64() | int64_t GetInt64() |
bool IsDouble() | double GetDouble() |
Document d; // Null
d.SetObject();
Value v; // Null
v.SetInt(10);
v = 10; // 简写,和上面的相同
方式二:
Value b(true); // 调用 Value(bool)
Value i(-123); // 调用 Value(int)
Value u(123u); // 调用 Value(unsigned)
Value d(1.5); // 调用 Value(double)
Value o(kObjectType); //object
Value a(kArrayType); //array
Value s;
s.SetString("rapidjson"); // 可包含空字符,长度在编译期推导
s = "rapidjson"; // 上行的缩写
包含接口:
Value a(kArrayType);
Document::AllocatorType& allocator = document.GetAllocator();
for (int i = 5; i <= 10; i++)
a.PushBack(i, allocator); // 可能需要调用 realloc() 所以需要 allocator
// 流畅接口(Fluent interface)
a.PushBack("Lua", allocator).PushBack("Mio", allocator);
Object 是键值对的集合。每个键必须为 String。要修改 Object,方法是增加或移除成员。以下的 API 用来增加成员:
Value contact(kObject);
contact.AddMember("name", "Milo", document.GetAllocator());
contact.AddMember("married", true, document.GetAllocator());
CopyFrom()
Swap()
Value a(123);
Value b("Hello");
a.Swap(b);
#include
#include"rapidjson/document.h"
#include"rapidjson/writer.h"
#include"rapidjson/stringbuffer.h"
#include
using namespace rapidjson;
using namespace std;
int main()
{
string strJsonTest = "{\"item_1\":\"value_1\",\"item_2\":\"value_2\",\"item_3\":\"value_3\",\"item_4\":\"value_4\",\"item_arr\":[\"arr_vaule_1\",\"arr_vaule_2\"]}";
Document docTest;
docTest.Parse<0>(strJsonTest.c_str());
if (!docTest.HasParseError())
{
for (rapidjson::Value::ConstMemberIterator itr = docTest.MemberBegin(); itr != docTest.MemberEnd(); itr++)
{
Value jKey;
Value jValue;
Document::AllocatorType allocator;
jKey.CopyFrom(itr->name, allocator);
jValue.CopyFrom(itr->value, allocator);
if (jKey.IsString())
{
string name = jKey.GetString();
printf("\r\nname: %s\r\n", name.c_str());
}
if (jValue.IsString())
{
std::cout << "jValue" << jValue.GetString() << std::endl;
}
}
}
return 0;
}