• rapidjson的read和write的sample


    头文件

    #include "json/document.h"
    #include "json/prettywriter.h"
    #include "json/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
    ",object.GetInt());
            }
        }
    }
    void test()
    {
        //read json
        string updateInfo = "{"UpdateInfo":[{"url":"aaaa.ipa","platform":"ios"}]}";
        
        rapidjson::Document doc;
        doc.Parse<0>(updateInfo.c_str());
    
        rapidjson::Value &dataArray = doc["UpdateInfo"];
    
        if (dataArray.IsArray())
        {
            for (int i = 0; i < dataArray.Size(); i++)
            {
                const rapidjson::Value& object = dataArray[i];
    
                string url = object["url"].GetString();
                string platform = object["platform"].GetString();
            }
        }
    
    
    
        //write json
        rapidjson::Document document;
        document.SetObject();
        rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
    
        rapidjson::Value array(rapidjson::kArrayType);
    
        for (int i = 0; i < 10; i++)
        {
            rapidjson::Value object(rapidjson::kObjectType);
            object.AddMember("id", 1, allocator);
            object.AddMember("name", "test", allocator);
            object.AddMember("version", 1.01, allocator);
            object.AddMember("vip", true, allocator);
    
    object.SetInt(i); array.PushBack(
    object, allocator); } document.AddMember("title", "PLAYER INFO", allocator); document.AddMember("players", array, allocator); rapidjson::StringBuffer buffer; rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); document.Accept(writer); auto out = buffer.GetString(); log("out: %s", out); }

    下面是单个的

    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
    ", d["carriorName"].GetString());
        printf("%s
    ", json);
    }
    void writeTest()
    {
        rapidjson::Document document;
        rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
        rapidjson::Value root(rapidjson::kObjectType);
        root.AddMember("name", "哥伦布啊", allocator);
        root.AddMember("gold",1234,allocator);
        rapidjson::StringBuffer buffer;
        rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
        root.Accept(writer);
        std::string reststring = buffer.GetString();
        std::cout << reststring << std::endl;
    }
  • 相关阅读:
    在 Eclipse Workbench 之外使用 Eclipse GUI
    GB2312,GBK,Unicode
    木偶一之推荐系统
    Matlab:任意矩阵计算分布密度(海明距离的分布密度)
    live555在arm linux下的交叉编译,并下载的Arm板播放H264文件
    java设计模式之原型模式
    HDU 1102
    poj3661另一种做法(滚动数组)
    基于QT的小游戏细菌病毒战
    某代码查看器的保护突破
  • 原文地址:https://www.cnblogs.com/ziyouchutuwenwu/p/3790322.html
Copyright © 2020-2023  润新知