rapidjson的使用

来源:互联网 发布:亲宝宝软件下载 编辑:程序博客网 时间:2024/05/18 05:58

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",        "CustomerID": false,        "Provinces": 123.45,        "City": 34545    }  ],  "customercontact": {    "CustomerContactID": "sample string 1",    "CustomerID": false,    "Provinces": 123.45,    "City": 34545  }}

解析json数据,详情见注释

#include "rapidjson/document.h"  #include "rapidjson/prettywriter.h"  #include "rapidjson/filestream.h"  #include "rapidjson/stringbuffer.h" #define psln(x) std::cout << #x " = " << (x) << std::endlusing std::string;    using std::ifstream;    string  stringFromStream;    ifstream  in;    in.open("E://json1.json", ifstream::in);    if (!in.is_open())        return;    string line;    while (getline(in, line)) {        stringFromStream.append(line + "\n");    }    in.close();    //cout << stringFromStream << endl;    Document doc;    doc.Parse<0>(stringFromStream.c_str());    if (doc.HasParseError()) {        //ParseErrorCode code = doc.GetParseError();        psln("error");        return;    }    // use values in parse result.    Value & v = doc["FlgStatus"];    if (v.IsString())    {        psln(v.GetString());    }    v = doc["OrderNo"];    if (v.IsBool())    {        psln(v.GetBool());    }    v = doc["CustomerContactID"];    if (v.IsDouble())    {        psln(v.GetDouble());    }    v = doc["CouponType"];    if (v.IsInt())    {        psln(v.GetInt());    }    //解析array    v = doc["ordersBody"];    if (v.IsArray())    {        int len = v.Size();        for (int i = 0; i < len; i++)        {            Value & value = v[i];            if (value.IsObject())            {                if (value.HasMember("CustomerContactID") && value["CustomerContactID"].IsString())                {                    psln(value["CustomerContactID"].GetString());                }                if (value.HasMember("CustomerID") && value["CustomerID"].IsBool())                {                    psln(value["CustomerID"].GetBool());                }                if (value.HasMember("Provinces") && value["Provinces"].IsDouble())                {                    psln(value["Provinces"].GetDouble());                }                if (value.HasMember("City") && value["City"].IsInt())                {                    psln(value["City"].GetInt());                }            }        }    }    //解析object    v = doc["customercontact"];    if (v.IsObject())    {        if (v.HasMember("CustomerContactID") && v["CustomerContactID"].IsString())        {            psln(v["CustomerContactID"].GetString());        }        if (v.HasMember("CustomerID") && v["CustomerID"].IsBool())        {            psln(v["CustomerID"].GetBool());        }        if (v.HasMember("Provinces") && v["Provinces"].IsDouble())        {            psln(v["Provinces"].GetDouble());        }        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", 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", CustomerID, allocatorType);    Value Provinces = 123.45;    customercontact.AddMember("Provinces", Provinces, allocatorType);    Value City = 34545;    customercontact.AddMember("City", City, allocatorType);    root.AddMember("customercontact", customercontact, allocatorType);    StringBuffer stringBuffer;    Writer<StringBuffer> writer(stringBuffer);    root.Accept(writer);    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();

更多详细教程

项目下载地址

0 0
原创粉丝点击