只讲编解码两个字符串:
strValue0 = "{\"status\":\"success\",\"success_ids\":[{\"msgid\":\"123\",\"rids\":[\"10000015f33d3831\"]}]}";
strValue1 = "{\"Failed_ids\":[{\"error_code\":\"401\",\"msgid\":\"efg\",\"rids\":[\"30000015f33d3831\"]}]}";
程序中要用到一些结构体和容器:原文链接:https://www.f2er.com/json/290508.htmlstruct data { string status; string msgid; string rids; string errcode; };vector<data> succVec
分别解出字符串中的成员。 编写的字符串为:{"Failed_ids":[{"error_code":"400","msgid":"abc","rids":["20000015f33d3831"]},{"error_code":"401","msgid":"efg","rids":["30000015f33d3831"]},{"error_code":"402","msgid":"hij","rids":["40000015f33d3831"]}],"status":"success","success_ids":[{"msgid":"123","rids":["10000015f33d3831"]},{"msgid":"456","rids":["10000015f33d3832"]},{"msgid":"789","rids":["10000015f33d3833"]}]}解码编码程序:
void decodeJson(const string& str,data& dataJson) { Json::Reader reader; Json::Value root; if(reader.parse(str,root)) { if(!root["status"].empty()) { dataJson.status = root["status"].asString(); } if(!root["success_ids"].empty()) { Json::Value sidsArrary = root["success_ids"]; for(int i = 0;i < sidsArrary.size();i++) { if(!sidsArrary[i]["rids"].empty()) { dataJson.msgid = sidsArrary[i]["msgid"].asString(); } if(!sidsArrary[i]["rids"].empty()) { Json::Value sridsArrary = sidsArrary[i]["rids"]; if(sridsArrary.isArray()) { Json::Value sridselement; for(int j = 0;j < sridsArrary.size();j++) { sridselement = sridsArrary[j]; dataJson.rids = sridselement.asString(); } } } } } if(!root["Failed_ids"].empty()) { Json::Value fidsArrary = root["Failed_ids"]; for(int p = 0;p < fidsArrary.size();p++) { if(!fidsArrary[p]["msgid"].empty()) { dataJson.msgid = fidsArrary[p]["msgid"].asString(); } if(!fidsArrary[p]["error_code"].empty()) { dataJson.errcode = fidsArrary[p]["error_code"].asString(); } if(!fidsArrary[p]["rids"].empty()) { Json::Value fridsArrary = fidsArrary[p]["rids"]; if(fridsArrary.isArray()) { Json::Value fridselement; for(int q = 0;q < fridsArrary.size();q++) { fridselement = fridsArrary[q]; dataJson.rids = fridselement.asString(); } } } } } } }void encodeJson(vector <data>& dataVec,string& strRet) { data dataJson; vector<data> succVec; vector<data> failVec; Json::Value succRoot,failRoot; Json::FastWriter writer; //Classify the message receipt : Successful receipt & Failful receipt vector<data>::iterator iter; for (iter = dataVec.begin();iter != dataVec.end();iter++) { dataJson = *iter; if(!dataJson.status.compare("success")) { succVec.push_back(dataJson); } else { failVec.push_back(dataJson); } } //Successful receipt coding if(0 < succVec.size()) { for (int i = 0;i < succVec.size();i++) { Json::Value sidsArrary; sidsArrary["msgid"] = succVec[i].msgid; sidsArrary["rids"].append(succVec[i].rids); succRoot["success_ids"].append(sidsArrary); succRoot["status"] = succVec[i].status; } } //Failful receipt coding if(0 < failVec.size()) { for (int i = 0;i < failVec.size();i++) { Json::Value fidsArrary; fidsArrary["error_code"] = failVec[i].errcode; fidsArrary["msgid"] = failVec[i].msgid; fidsArrary["rids"].append(failVec[i].rids); failRoot["Failed_ids"].append(fidsArrary); } } //Integrated coding if((!succRoot.empty()) && (!failRoot.empty())) { succRoot["Failed_ids"] = failRoot["Failed_ids"]; strRet = writer.write(succRoot); } else if((!succRoot.empty()) && failRoot.empty()) { strRet = writer.write(succRoot); } else if(succRoot.empty() && (!failRoot.empty())) { strRet = writer.write(failRoot); } }