加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Jsoncpp的编解码---高级篇

发布时间:2020-12-16 19:23:47 所属栏目:百科 来源:网络整理
导读:只讲编解码两个字符串: strValue0 = "{"status":"success","success_ids":[{"msgid":"123","rids":["10000015f33d3831"]}]}"; strValue1 = "{"failed_ids":[{"error_code":"401","msgid":"efg","rids":["30000015f33d3831"]}]}
 

只讲编解码两个字符串:

strValue0 = "{"status":"success","success_ids":[{"msgid":"123","rids":["10000015f33d3831"]}]}";

strValue1 = "{"failed_ids":[{"error_code":"401","msgid":"efg","rids":["30000015f33d3831"]}]}";

程序中要用到一些结构体和容器:
struct 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);
	}	
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读