C++项目RapidJson的详细用法总结

本文基于2018年1月最新仓库代码撰写
仓库: https://github.com/Tencent/rapidjson
用法: 将’include/rapidjson’文件夹copy到工程内直接使用
Android ndk开发会增大apk体积1.4M, 当然, 肯定比自己写的要快

重要的内容在前面

本例Android工程地址

https://github.com/HOLDfoot/RapidJsonAndroidSample
代码请见native-lib.cpp文件

获取构造后的json字符串, 通过buffer.GetString(), 转换成C++的string对象的方式如下

    std::string json = std::string(buffer.GetString());

打印json

可以通过C++ string的c_str()也可以直接通过buffer.GetString(), 该buffer是rapidjson::StringBuffer类的对象

用法Zero: 构造复杂Json(网上2015年的例子没法用)

 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 writer(buffer);
    document.Accept(writer);
    std::string json = std::string(buffer.GetString());
    LOGD("testAddMember = %s", json.c_str());

用法One: 简单修改json字符串

    // 1. 把 JSON 解析至 DOM。
    const char* json = "{\"project\":\"rapidjson\",\"stars\":10}";
    Document d;
    d.Parse(json);
    // 2. 利用 DOM 作出修改。
    Value& s = d["stars"];
    s.SetInt(s.GetInt() + 1);
    // 3. 把 DOM 转换(stringify)成 JSON。
    StringBuffer buffer;
    Writer writer(buffer);
    d.Accept(writer);
    // Output {"project":"rapidjson","stars":11}
    std::cout << buffer.GetString() << std::endl;

用法Two: 构造简单json

  StringBuffer s;
    Writer 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();
    // {"hello":"world","t":true,"f":false,"n":null,"i":123,"pi":3.1416,"a":[0,1,2,3]}
    std::cout << s.GetString() << std::endl;

你可能感兴趣的:(C++项目RapidJson的详细用法总结)