rapidjson的read和write的sample

前端之家收集整理的这篇文章主要介绍了rapidjson的read和write的sample前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#include "json/document.h"
#include json/prettywriter.hjson/stringbuffer.h"

这是生成数组的

void test()
{
    string info = {\"id\":\"1111111\",\"cards\":[0,1,2,3,4,5,6,7,8,9],\"jiaoDiZhu\":\"\"}";

    rapidjson::Document doc;
    doc.Parse<0>(info.c_str());

    rapidjson::Value &dataArray = doc[cards"];

    if (dataArray.IsArray())
    {
        for (rapidjson::SizeType i = 0; i < dataArray.Size(); i++)
        {
            const rapidjson::Value& object = dataArray[i];

            printf(%d\n",object.GetInt());
        }
    }
}

//read json string updateInfo = {\"UpdateInfo\":[{\"url\":\"aaaa.ipa\",\"platform\":\"ios\"}]}"; rapidjson::Document doc; doc.Parse<0>(updateInfo.c_str()); rapidjson::Value &dataArray = doc[UpdateInfofor (int i = object = dataArray[i]; string url = object[url"].GetString(); string platform = platform"].GetString(); } } write json rapidjson::Document document; document.SetObject(); rapidjson::Document::AllocatorType& allocator = document.GetAllocator(); rapidjson::Value array(rapidjson::kArrayType); 0; i < 10; i++) { rapidjson::Value object(rapidjson::kObjectType); object.AddMember(id1,allocator); nametest",0); line-height:1.5!important">version1.01,0); line-height:1.5!important">viptrue,allocator); object.SetInt(i); array.PushBack(object,allocator); } document.AddMember(titlePLAYER INFOplayers writer(buffer); document.Accept(writer); auto out = buffer.GetString(); log(out: %sout); }

下面是单个的

void readTest() { char json[] = {\"carriorName\":\"日本电信\",\"deviceName\":\"iPhone5,2\",\"osName\":\"android\",\"osVersion\":\"8.1\",\"appId\":\"com.mmcshadow.doudizhu\",\"versionName\":\"1.0\",\"versionCode\":\"1.0\",\"deviceId\":\"11111111\"}"; rapidjson::Document d; d.Parse<0>(json); printf(%s\ncarriorName"].GetString()); printf(
void writeTest() { rapidjson::Document document; rapidjson::Document::AllocatorType& allocator = document.GetAllocator(); rapidjson::Value root(rapidjson::kObjectType); root.AddMember(哥伦布啊gold1234,allocator); rapidjson::StringBuffer buffer; rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); root.Accept(writer); std::string reststring = buffer.GetString(); std::cout << reststring << std::endl; }

猜你在找的Json相关文章