byte用json存 c++_C++ Json数据保存与解析(到文件)

1 #include"stdafx.h"2 #include3 #include"Json.h"4 5 #include"ShopLogic.h"6 #include"src/FileCsv/WebUrlConfig.h"7 #include"src/support/GlobalDefine.h"8 9 usingnamespacestd;10 11 constchar*g_pcszWebDataSaveFile="shopWebData_";12 13 ///14 ///class CShopData15 ///16 CShopData*getShopData() {17 returnChaSingleton::singleton();18 }19 20 CShopData::CShopData() {21 22 }23 24 CShopData::~CShopData() {25 26 }27 28 ///29 ///Add by (jacc.kim) [2014-10-16 17:01]30 ///将从web下发的商品列表数据保存到本地配置中.注意:如果列表为空,则不保存,31 ///同时旧有的本地数据也不清空。32 ///33 voidCShopData::saveShopWebItems(constTShopWebDataVec&vecItems) {34 if(vecItems.empty()) {35 return;36 }37 ShopWebDataLog("========================================================");38 ShopWebDataLog("准备保存宝石商店数据到本地文件.");39 std::stringstrFileName("");40 this->generateWebDataSaveFileName(strFileName);41 ShopWebDataLog("保存数据文件名称: %s", strFileName.c_str());42 ofstream fout;43 fout.open(strFileName.c_str());44 if(!fout.is_open()) {45 ShopWebDataLog("打开或创建文件失败");46 return;47 }48 Json::Value jRoot;49 Json::Value jArray;50 Json::Value jItem;51 Json::Value jHttpData;52 Json::Value jTreasureInfo;53 typedef Json::Value::Int        TJInt;54 55 CShopHttpDataTotal*pWebDataItem=NULL;56 constTShopWebDataVec::size_type nAmount=vecItems.size();57 for(TShopWebDataVec::size_type nIdx=0; nIdxhttpData.pc_id.c_str();64 jHttpData["pc_name"]=pWebDataItem->httpData.pc_name.c_str();65 jHttpData["pc_point"]=(TJInt)pWebDataItem->httpData.pc_point;66 jHttpData["pc_type"]=pWebDataItem->httpData.pc_type.c_str();67 jHttpData["pc_flag"]=(TJInt)pWebDataItem->httpData.pc_flag;68 jHttpData["pc_credit_rate"]=pWebDataItem->httpData.pc_credit_rate.c_str();69 jHttpData["pcc_price_usd"]=pWebDataItem->httpData.pcc_price_usd.c_str();70 jHttpData["pcc_price_cfg"]=pWebDataItem->httpData.pcc_price_cfg.c_str();71 jHttpData["pcc_memo"]=pWebDataItem->httpData.pcc_memo.c_str();72 jHttpData["pc_free_point"]=(TJInt)pWebDataItem->httpData.pc_free_point;73 jHttpData["ppc_end_time"]=pWebDataItem->httpData.ppc_end_time.c_str();74 #ifdef ORDERS_PROMOTIONS75 jHttpData["pcc_presen_count"]=(TJInt)pWebDataItem->httpData.pcc_presen_count;76 #else//#ifdef ORDERS_PROMOTIONS77 jHttpData["pcc_presen_count"]=(TJInt)0;78 #endif//#ifdef ORDERS_PROMOTIONS79 80 //保存 商城财产相关信息(即:TreasureInfo) 数据81 jTreasureInfo["nIconStatus"]=(TJInt)pWebDataItem->localData.nIconStatus;82 jTreasureInfo["nTextStatus"]=(TJInt)pWebDataItem->localData.nTextStatus;83 jTreasureInfo["nBuyType"]=(TJInt)pWebDataItem->localData.nBuyType;84 jTreasureInfo["nCostType"]=(TJInt)pWebDataItem->localData.nCostType;85 jTreasureInfo["nBuyNumber"]=(TJInt)pWebDataItem->localData.nBuyNumber;86 jTreasureInfo["nCostNumber"]=(TJInt)pWebDataItem->localData.nCostNumber;87 jTreasureInfo["nCapacityPercentage"]=(TJInt)pWebDataItem->localData.nCapacityPercentage;88 jTreasureInfo["strName"]=pWebDataItem->localData.strName.c_str();89 jTreasureInfo["strTitleTid"]=pWebDataItem->localData.strTitleTid.c_str();90 jTreasureInfo["strIconAddress"]=pWebDataItem->localData.strIconAddress.c_str();91 jTreasureInfo["nCooling"]=(TJInt)pWebDataItem->localData.nCooling;92 93 jItem["CShopHttpData"]=jHttpData;94 jItem["TreasureInfo"]=jTreasureInfo;95 jArray.append(jItem);96 }97 ShopWebDataLog("待保存的宝石商店数据: %d 条, 实际保存成功: %d 条"98 , static_cast(nAmount), static_cast(jArray.size()));99 jRoot["CShopHttpDataTotal"]=jArray;100 fout<generateWebDataSaveFileName(strFileName);115 ShopWebDataLog("本地数据源文件名称: %s", strFileName.c_str());116 ifstream fin;117 fin.open(strFileName.c_str());118 if(!fin.is_open()) {119 ShopWebDataLog("本地数据源文件不存在或已损坏,打开失败");120 return;121 }122 Json::Reader jReader;123 Json::Value jRoot;124 Json::Value jHttpData;125 Json::Value jTreasureInfo;126 if(!jReader.parse(fin, jRoot)) {127 ShopWebDataLog("本地数据源文件糟破坏,解析失败");128 return;129 }130 CShopHttpDataTotal*pWebDataItem=NULL;131 constintnAmount=static_cast(jRoot["CShopHttpDataTotal"].size());132 for(intnIdx=0; nIdxhttpData.pc_id=jHttpData["pc_id"].asString();139 pWebDataItem->httpData.pc_name=jHttpData["pc_name"].asString();140 pWebDataItem->httpData.pc_point=jHttpData["pc_point"].asInt();141 pWebDataItem->httpData.pc_type=jHttpData["pc_type"].asString();142 pWebDataItem->httpData.pc_flag=jHttpData["pc_flag"].asInt();143 pWebDataItem->httpData.pc_credit_rate=jHttpData["pc_credit_rate"].asString();144 pWebDataItem->httpData.pcc_price_usd=jHttpData["pcc_price_usd"].asString();145 pWebDataItem->httpData.pcc_price_cfg=jHttpData["pcc_price_cfg"].asString();146 pWebDataItem->httpData.pcc_memo=jHttpData["pcc_memo"].asString();147 pWebDataItem->httpData.pc_free_point=jHttpData["pc_free_point"].asInt();148 pWebDataItem->httpData.ppc_end_time=jHttpData["ppc_end_time"].asString();149 #ifdef ORDERS_PROMOTIONS150 pWebDataItem->httpData.pcc_presen_count=jHttpData["pcc_presen_count"].asInt();151 #else//#ifdef ORDERS_PROMOTIONS152 //do nothing here

..153 #endif//#ifdef ORDERS_PROMOTIONS154 155 jTreasureInfo=jRoot["CShopHttpDataTotal"][nIdx]["TreasureInfo"];156 pWebDataItem->localData.nIconStatus=jTreasureInfo["nIconStatus"].asInt();157 pWebDataItem->localData.nTextStatus=jTreasureInfo["nTextStatus"].asInt();158 pWebDataItem->localData.nBuyType=jTreasureInfo["nBuyType"].asInt();159 pWebDataItem->localData.nCostType=jTreasureInfo["nCostType"].asInt();160 pWebDataItem->localData.nBuyNumber=jTreasureInfo["nBuyNumber"].asInt();161 pWebDataItem->localData.nCostNumber=jTreasureInfo["nCostNumber"].asInt();162 pWebDataItem->localData.nCapacityPercentage=jTreasureInfo["nCapacityPercentage"].asInt();163 pWebDataItem->localData.strName=jTreasureInfo["strName"].asString();164 pWebDataItem->localData.strTitleTid=jTreasureInfo["strTitleTid"].asString();165 pWebDataItem->localData.strIconAddress=jTreasureInfo["strIconAddress"].asString();166 pWebDataItem->localData.nCooling=jTreasureInfo["nCooling"].asInt();167 168 vecOutItems.push_back(pWebDataItem);169 }170 ShopWebDataLog("本地数据源共: %d 条数据, 实际解析成功: %d 条"171 , nAmount, static_cast(vecOutItems.size()));172 fin.close();173 }174 175 voidCShopData::generateWebDataSaveFileName(std::string&strFileName) {176 strFileName=g_pcszWebDataSaveFile;177 CWebUrlConfig*pUrlCfg=CWebUrlConfigInstance::singleton();178 std::stringstrGameId=pUrlCfg->getWebUrlAddress("gameid");179 strFileName=strFileName+strGameId+".json";180 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值