代码如下:
ParseJson* ParseJson::createWithString(std::string data) {
ParseJson *pRet = new ParseJson();
if (pRet->initWithString(data)) {
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return nullptr;
}
bool ParseJson::initWithString(std::string data) {
this->content = data;
return true;
}
void ParseJson::parse() {
rapidjson::Document document;
//解码content进行有效性检测,0为默认值
document.Parse<0>(content.c_str());
//判断解析是否有错
CCASSERT(!document.HasParseError(), "Parsing to document failure");
log("Parsing to document succeeded");
CC_ASSERT(document.IsObject() && document.HasMember("Record"));
//获取document中Record后信息
const rapidjson::Value &records = document["Record"];
CC_ASSERT(records.IsArray());
for (unsigned int i = 0; i < records.Size(); i++) {
row = ValueMap();
//获取record对象
const rapidjson::Value &record = records[i];
//动作
if (record.HasMember("action")) {
const rapidjson::Value &v_action = record["action"];
CC_ASSERT(v_action.IsString());
row["action"] = Value(v_action.GetString());
}
//用户名
if (record.HasMember("userID")) {
const rapidjson::Value &v_userid = record["userID"];
CC_ASSERT(v_userid.IsString());
row["userID"] = Value(v_userid.GetString());
}
//密码
if (record.HasMember("password")) {
const rapidjson::Value &v_password = record["password"];
CC_ASSERT(v_password.IsString());
row["password"] = Value(v_password.GetString());
}
//clientID
if (record.HasMember("clientID")) {
const rapidjson::Value &v_clientID = record["clientID"];
CC_ASSERT(v_clientID.IsString());
row["clientID"] = Value(v_clientID.GetString());
}
//账户余额
if (record.HasMember("account")) {
const rapidjson::Value &v_account = record["account"];
CC_ASSERT(v_account.IsString());
row["account"] = Value(v_account.GetString());
}
//动作目标
if (record.HasMember("target")) {
const rapidjson::Value &v_target = record["target"];
CC_ASSERT(v_target.IsString());
row["target"] = Value(v_target.GetString());
}
//是否存在好友
if (record.HasMember("isExist")) {
const rapidjson::Value &v_isExist = record["isExist"];
CC_ASSERT(v_isExist.IsString());
row["isExist"] = Value(v_isExist.GetString());
}
//添加删除结果
if (record.HasMember("result")) {
const rapidjson::Value &v_result = record["result"];
CC_ASSERT(v_result.IsString());
row["result"] = Value(v_result.GetString());
}
//好友ID
if (record.HasMember("friendID")) {
const rapidjson::Value &v_friendID = record["friendID"];
CC_ASSERT(v_friendID.IsString());
row["friendID"] = Value(v_friendID.GetString());
}
//卡牌ID
if (record.HasMember("cardID")) {
const rapidjson::Value &v_cardID = record["cardID"];
CC_ASSERT(v_cardID.IsString());
row["cardID"] = Value(v_cardID.GetString());
}
//卡组ID
if (record.HasMember("groupID")) {
const rapidjson::Value &v_groupID = record["groupID"];
CC_ASSERT(v_groupID.IsString());
row["groupID"] = Value(v_groupID.GetString());
}
//卡包数目
if (record.HasMember("packageNumber")) {
const rapidjson::Value &v_packageNumber = record["packageNumber"];
CC_ASSERT(v_packageNumber.IsString());
row["packageNumber"] = Value(v_packageNumber.GetString());
}
//是否可以购买
if (record.HasMember("canPurchase")) {
const rapidjson::Value &v_canPurchase = record["canPurchase"];
CC_ASSERT(v_canPurchase.IsString());
row["purchaseCost"] = Value(v_canPurchase.GetString());
}
//花费
if (record.HasMember("account")) {
const rapidjson::Value &v_account = record["account"];
CC_ASSERT(v_account.IsString());
row["cost"] = Value(v_account.GetString());
}
//战役ID
if (record.HasMember("campaignID")) {
const rapidjson::Value &v_campaignID = record["campaignID"];
CC_ASSERT(v_campaignID.IsString());
row["campaignID"] = Value(v_campaignID.GetString());
}
//得到金钱
if (record.HasMember("coinAward")) {
const rapidjson::Value &v_coinAward = record["coinAward"];
CC_ASSERT(v_coinAward.IsString());
row["coinAward"] = Value(v_coinAward.GetString());
}
//得到卡牌
if (record.HasMember("cardAward")) {
const rapidjson::Value &v_cardAward = record["cardAward"];
CC_ASSERT(v_cardAward.IsString());
row["cardAward"] = Value(v_cardAward.GetString());
}
//战斗结束
if (record.HasMember("finish")) {
const rapidjson::Value &v_finish = record["finish"];
CC_ASSERT(v_finish.IsString());
row["finish"] = Value(v_finish.GetString());
}
//收到消息
if (record.HasMember("message")) {
const rapidjson::Value &v_message = record["message"];
CC_ASSERT(v_message.IsString());
row["message"] = Value(v_message.GetString());
}
/*
继续添加
*/
list.push_back(Value(row));
}
}
ParseJson* ParseJson::createWithString(std::string data) {
ParseJson *pRet = new ParseJson();
if (pRet->initWithString(data)) {
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return nullptr;
}
bool ParseJson::initWithString(std::string data) {
this->content = data;
return true;
}
void ParseJson::parse() {
rapidjson::Document document;
//解码content进行有效性检测,0为默认值
document.Parse<0>(content.c_str());
//判断解析是否有错
CCASSERT(!document.HasParseError(), "Parsing to document failure");
log("Parsing to document succeeded");
CC_ASSERT(document.IsObject() && document.HasMember("Record"));
//获取document中Record后信息
const rapidjson::Value &records = document["Record"];
CC_ASSERT(records.IsArray());
for (unsigned int i = 0; i < records.Size(); i++) {
row = ValueMap();
//获取record对象
const rapidjson::Value &record = records[i];
//动作
if (record.HasMember("action")) {
const rapidjson::Value &v_action = record["action"];
CC_ASSERT(v_action.IsString());
row["action"] = Value(v_action.GetString());
}
//用户名
if (record.HasMember("userID")) {
const rapidjson::Value &v_userid = record["userID"];
CC_ASSERT(v_userid.IsString());
row["userID"] = Value(v_userid.GetString());
}
//密码
if (record.HasMember("password")) {
const rapidjson::Value &v_password = record["password"];
CC_ASSERT(v_password.IsString());
row["password"] = Value(v_password.GetString());
}
//clientID
if (record.HasMember("clientID")) {
const rapidjson::Value &v_clientID = record["clientID"];
CC_ASSERT(v_clientID.IsString());
row["clientID"] = Value(v_clientID.GetString());
}
//账户余额
if (record.HasMember("account")) {
const rapidjson::Value &v_account = record["account"];
CC_ASSERT(v_account.IsString());
row["account"] = Value(v_account.GetString());
}
//动作目标
if (record.HasMember("target")) {
const rapidjson::Value &v_target = record["target"];
CC_ASSERT(v_target.IsString());
row["target"] = Value(v_target.GetString());
}
//是否存在好友
if (record.HasMember("isExist")) {
const rapidjson::Value &v_isExist = record["isExist"];
CC_ASSERT(v_isExist.IsString());
row["isExist"] = Value(v_isExist.GetString());
}
//添加删除结果
if (record.HasMember("result")) {
const rapidjson::Value &v_result = record["result"];
CC_ASSERT(v_result.IsString());
row["result"] = Value(v_result.GetString());
}
//好友ID
if (record.HasMember("friendID")) {
const rapidjson::Value &v_friendID = record["friendID"];
CC_ASSERT(v_friendID.IsString());
row["friendID"] = Value(v_friendID.GetString());
}
//卡牌ID
if (record.HasMember("cardID")) {
const rapidjson::Value &v_cardID = record["cardID"];
CC_ASSERT(v_cardID.IsString());
row["cardID"] = Value(v_cardID.GetString());
}
//卡组ID
if (record.HasMember("groupID")) {
const rapidjson::Value &v_groupID = record["groupID"];
CC_ASSERT(v_groupID.IsString());
row["groupID"] = Value(v_groupID.GetString());
}
//卡包数目
if (record.HasMember("packageNumber")) {
const rapidjson::Value &v_packageNumber = record["packageNumber"];
CC_ASSERT(v_packageNumber.IsString());
row["packageNumber"] = Value(v_packageNumber.GetString());
}
//是否可以购买
if (record.HasMember("canPurchase")) {
const rapidjson::Value &v_canPurchase = record["canPurchase"];
CC_ASSERT(v_canPurchase.IsString());
row["purchaseCost"] = Value(v_canPurchase.GetString());
}
//花费
if (record.HasMember("account")) {
const rapidjson::Value &v_account = record["account"];
CC_ASSERT(v_account.IsString());
row["cost"] = Value(v_account.GetString());
}
//战役ID
if (record.HasMember("campaignID")) {
const rapidjson::Value &v_campaignID = record["campaignID"];
CC_ASSERT(v_campaignID.IsString());
row["campaignID"] = Value(v_campaignID.GetString());
}
//得到金钱
if (record.HasMember("coinAward")) {
const rapidjson::Value &v_coinAward = record["coinAward"];
CC_ASSERT(v_coinAward.IsString());
row["coinAward"] = Value(v_coinAward.GetString());
}
//得到卡牌
if (record.HasMember("cardAward")) {
const rapidjson::Value &v_cardAward = record["cardAward"];
CC_ASSERT(v_cardAward.IsString());
row["cardAward"] = Value(v_cardAward.GetString());
}
//战斗结束
if (record.HasMember("finish")) {
const rapidjson::Value &v_finish = record["finish"];
CC_ASSERT(v_finish.IsString());
row["finish"] = Value(v_finish.GetString());
}
//收到消息
if (record.HasMember("message")) {
const rapidjson::Value &v_message = record["message"];
CC_ASSERT(v_message.IsString());
row["message"] = Value(v_message.GetString());
}
/*
继续添加
*/
list.push_back(Value(row));
}
}