rapidjsonの使用


rapidjson json 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::endl

using 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 + "
"
); } 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();

詳細チュートリアル
プロジェクトのダウンロードアドレス