rapidjson的使用

前端之家收集整理的这篇文章主要介绍了rapidjson的使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

rapidjson用来解析和生成json数据
1、解析json数据
json数据如下图

{
  "FlgStatus": "sample string 1","OrderNo": true,"CustomerContactID": 123.45,"CouponType": 1233435454,"ordersBody": [ { "CustomerContactID": "sample string 1","CustomerID": false,"Provinces": 123.45,"City": 34545 },{ "CustomerContactID": "sample string 1","City": 34545 } ],"customercontact": { "CustomerContactID": "sample string 1","City": 34545 } }

解析json数据,详情见注释

#include "rapidjson/document.h" 
#include "rapidjson/prettywriter.h" 
#include "rapidjson/filestream.h" 
#include "rapidjson/stringbuffer.h" 

#@H_404_99@define psln(x) std::cout << #x " = " << (x) << std::endl

@H_404_99@using std::@H_404_99@string;
    @H_404_99@using std::ifstream;

    @H_404_99@string  stringFromStream;
    ifstream  @H_404_99@in;
    @H_404_99@in.open("E://json1.json",ifstream::@H_404_99@in);
    @H_404_99@if (!@H_404_99@in.is_open())
        @H_404_99@return;
    @H_404_99@string line;
    @H_404_99@while (getline(@H_404_99@in,line)) {
        stringFromStream.append(line + "\n");
    }
    @H_404_99@in.close();

    //cout << stringFromStream << endl;

    Document doc;
    doc.Parse<0>(stringFromStream.c_str());
    @H_404_99@if (doc.HasParseError()) {
        //ParseErrorCode code = doc.GetParseError();
        psln("error");
        @H_404_99@return;
    }

    // use values in parse result.
    Value & v = doc["FlgStatus"];
    @H_404_99@if (v.IsString())
    {
        psln(v.GetString());
    }
    v = doc["OrderNo"];
    @H_404_99@if (v.IsBool())
    {
        psln(v.GetBool());
    }
    v = doc["CustomerContactID"];
    @H_404_99@if (v.IsDouble())
    {
        psln(v.GetDouble());
    }
    v = doc["CouponType"];
    @H_404_99@if (v.IsInt())
    {
        psln(v.GetInt());
    }
    //解析array
    v = doc["ordersBody"];
    @H_404_99@if (v.IsArray())
    {
        @H_404_99@int len = v.Size();
        @H_404_99@for (@H_404_99@int i = 0; i < len; i++)
        {
            Value & @H_404_99@value = v[i];
            @H_404_99@if (@H_404_99@value.IsObject())
            {
                @H_404_99@if (@H_404_99@value.HasMember("CustomerContactID") && @H_404_99@value["CustomerContactID"].IsString())
                {
                    psln(@H_404_99@value["CustomerContactID"].GetString());
                }
                @H_404_99@if (@H_404_99@value.HasMember("CustomerID") && @H_404_99@value["CustomerID"].IsBool())
                {
                    psln(@H_404_99@value["CustomerID"].GetBool());
                }
                @H_404_99@if (@H_404_99@value.HasMember("Provinces") && @H_404_99@value["Provinces"].IsDouble())
                {
                    psln(@H_404_99@value["Provinces"].GetDouble());
                }
                @H_404_99@if (@H_404_99@value.HasMember("City") && @H_404_99@value["City"].IsInt())
                {
                    psln(@H_404_99@value["City"].GetInt());
                }
            }
        }
    }
    //解析object
    v = doc["customercontact"];
    @H_404_99@if (v.IsObject())
    {
        @H_404_99@if (v.HasMember("CustomerContactID") && v["CustomerContactID"].IsString())
        {
            psln(v["CustomerContactID"].GetString());
        }
        @H_404_99@if (v.HasMember("CustomerID") && v["CustomerID"].IsBool())
        {
            psln(v["CustomerID"].GetBool());
        }
        @H_404_99@if (v.HasMember("Provinces") && v["Provinces"].IsDouble())
        {
            psln(v["Provinces"].GetDouble());
        }
        @H_404_99@if (v.HasMember("City") && v["City"].IsInt())
        {
            psln(v["City"].GetInt());
        }
    }

2、生成json数据,格式和上面一样

Document document;
    Document::AllocatorType& allocatorType = document.GetAllocator();
    Value root(kObjectType);

    //添加string数据
    Value FlgStatus="sample string 1";
    root.AddMember("FlgStatus",FlgStatus,allocatorType);
    //添加bool
    Value OrderNo=true;
    root.AddMember("OrderNo",OrderNo,allocatorType);
    //添加int
    Value CustomerContactID=123;
    root.AddMember("CustomerContactID",CustomerContactID,allocatorType);
    //添加double
    Value CouponType = 123.456;
    root.AddMember("CouponType",CouponType,allocatorType);
    //添加array
    Value ordersBody(kArrayType);
    for (int i = 0; i < 2; i++)
    {
        Value v(kObjectType);

        Value CustomerContactID = "sample string 1";
        v.AddMember("CustomerContactID",allocatorType);
        Value CustomerID = false;
        v.AddMember("CustomerID",CustomerID,allocatorType);
        Value Provinces = 123.45;
        v.AddMember("Provinces",Provinces,allocatorType);
        Value City = 34545;
        v.AddMember("City",City,allocatorType);

        ordersBody.PushBack(v,allocatorType);
    }
    root.AddMember("ordersBody",ordersBody,allocatorType);

    //添加object数据
    Value customercontact(kObjectType);
    Value CustomerContactID1 = "sample string 1";
    customercontact.AddMember("CustomerContactID",CustomerContactID1,allocatorType);
    Value CustomerID = false;
    customercontact.AddMember("CustomerID",allocatorType);
    Value Provinces = 123.45;
    customercontact.AddMember("Provinces",allocatorType);
    Value City = 34545;
    customercontact.AddMember("City",allocatorType);
    root.AddMember("customercontact",customercontact,allocatorType);


    StringBuffer stringBuffer;
    Writer<StringBuffer> writer(stringBuffer);
    root.Accept(writer);
    @H_404_99@std::string result = stringBuffer.GetString();
    cout << "result: " << result.c_str() << "..........:" << result.size() << endl;

    ofstream outfile("E://json2.json");
    outfile << result.c_str() << endl;
    outfile.close();

更多详细教程

项目下载地址

猜你在找的Json相关文章