rapidjson write & read

目标json:

{
  "result" : 1,
  "data" : {
    "friends" : [
      {
        "Award" : 2,
        "friend" : {
          "gate" : 0,
          "nickName" : "游客111111",
          "uuid" : "111111"
        }
      }
    ],
    "invited" : true
  }
}

rapidjson code:

#include "rapidjson.h"
#include "stringbuffer.h"
#include "writer.h"
using namespace rapidjson;

    Document doc;
    doc.SetObject();
    Document::AllocatorType& all = doc.GetAllocator();
    doc.AddMember("result",1,all);
        Value data;
        data.SetObject();
            Value friends(kArrayType);
            for (int i = 0; i < 1; i++) {
                Value friendsData;
                friendsData.SetObject();
                friendsData.AddMember("Award",2,all);
                        Value friendData;
                        friendData.SetObject();
                        friendData.AddMember("gate",0,all);
                        friendData.AddMember("uuid","111111",all);
                        friendData.AddMember("nickName","游客111111",all);
                friendsData.AddMember("friend",friendData,all);
                friends.PushBack(friendsData,all);
            }
        data.AddMember("friends",friends,all);
        data.AddMember("invited",true,all);
    doc.AddMember("data",data,all);
    rapidjson::StringBuffer buffer;
    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
    doc.Accept(writer);
    std::string dataString = buffer.GetString();


解析目标json:


    Document jsonDic;
    string str = "目标JSON";
    jsonDic.Parse<0>(str);
    Value &dataValue= jsonDic["data"];
    int isInvited = dataValue["invited"].GetBool();
    
    Value &friendsData = dataValue["friends"];
    if (friendsData.IsArray()) {
        for (rapidjson::SizeType i = 0; i < friendsData.Size(); i++)
        {
            Value &friendData = friendsData[i];
            int invitedAward = friendData["Award"].GetInt();
            Value &friendObject = friendData["friend"];
            int tollgate = friendObject["gate"].GetInt();
            string uuid = friendObject["uuid"].GetString();
            string nickName = friendObject["nickName"].GetString();
        }
    }


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dd135373/article/details/41805145
文章标签: cocos2d-x json rapidjson
个人分类: cocos2d-x
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭