1)基本數(shù)據(jù)類(lèi)型的讀取
{
"name": "fengyuzaitu",
"age": 29.5,
"married": false,
"utcsecond": 1548745708,
"utcmilisecond":1548745708000
}
剛察ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書(shū)銷(xiāo)售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書(shū)合作)期待與您的合作!
std::ifstream ifs;
ifs.open("testdata.json", std::ios::binary);
Json::Reader reader(Json::Features::strictMode());
Json::Value root;
if (NULL == reader.parse(ifs, root))
{
ifs.close();
return;
}
ifs.close();
if (!root["name"].isString()) return;
if (!root["age"].isDouble()) return;
if (!root["married"].isBool()) return;
if (!root["utcsecond"].isInt()) return;
if (!root["utcmilisecond"].isInt64()) return;
std::string strName = root["name"].asString();
double dAge = root["age"].asDouble();
bool bMarried = root["married"].asBool();
int32_t nUtcSecond = root["utcsecond"].asInt();
int64_t nUtcMiliSecond = root["utcmilisecond"].asInt64();
2)字符串?dāng)?shù)組
{
"success":false,
"toReturn":[
{
"createTime":"20080806114526000+0800",
"createUser":"張三"
},
{
"createTime":"20080806114526000+0801",
"createUser":"李四"
}
],
"total":2
}
std::string strTotalNumber = root["total"].asString();
Json::Value subValue = root["toReturn"];
if (subValue.isNull()) return;
size_t count = subValue.size();
for (size_t i = 0; i < count; i++)
{
std::string strCreateTime = subValue[i]["createTime"].asString();
std::string strCreateUser = subValue[i]["createUser"].asString();
}
3)數(shù)據(jù)內(nèi)嵌json文本
一般情況下如下是一個(gè)正常的json文本
{
"msg":{
"absTime":1521699455000,
"ext":{"vehicleColor":"A","monitorId":"85ee85b052dd228a08494e11858060f4"}
},
"systemMessage":"1"
}
但是如下,將ext的內(nèi)容通過(guò)雙引號(hào)包含,變成了一個(gè)字符串,解析就需要特別注意
文本內(nèi)容:
{
"msg":
{
"absTime": 1521699455000,
"ext": "{\"vehicleColor\":\"A\",\"monitorId\":\"85ee85b052dd228a08494e11858060f4\"}"
},
"systemMessage": "1"
}內(nèi)存查看
{
"msg":
{
"absTime": 1521699455000,
"ext": "{\"vehicleColor\":\"A\",\"monitorId\":\"85ee85b052dd228a08494e11858060f4\"}"
},
"systemMessage": "1"
}
通過(guò)轉(zhuǎn)義來(lái)區(qū)分雙引號(hào),第一步獲取到ext對(duì)應(yīng)的字符串,然后重新解析該字符串
Json::Value msg;
Json::Reader readerMsg(Json::Features::strictMode());
if(NULL == readerMsg.parse(strMsg, msg) return;
std::string strExt = msg["ext"].asString();
Json::Value ext;
Json::Reader readerExt(Json::Features::strictMode());
if (readerer.parse(strExt, ext))
{
std::string strColor = ext["vehicleColor"].asString();
std::string strMonitorId = ext["monitorId"].asString();
}
注意:提示字符串中字符無(wú)效
jsoncpp 在處理 UTF-8 編碼的字符串是沒(méi)有問(wèn)題,意思是可以正確的解析出鍵值對(duì),在VS調(diào)試情況下,返回的字符串如果包含中文,會(huì)提示:字符串中字符無(wú)效,這是因?yàn)樵赩S調(diào)試過(guò)程中,只支持GBK編碼的中文,因此如果需要進(jìn)行字符串的查看,觀察字符串的中文含義,可以先將字符串從UTF-8編碼轉(zhuǎn)換為GBK編碼,然后就可以進(jìn)行變量的監(jiān)視提供UTF-8轉(zhuǎn)GBK
std::string UTF8ToGBK(const char* szUTF8)
{
int len = MultiByteToWideChar(CP_UTF8, 0, szUTF8, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_UTF8, 0, szUTF8, -1, wstr, len);
len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
char* pszStr = new char[len + 1];
memset(pszStr, 0, len + 1);
WideCharToMultiByte(CP_ACP, 0, wstr, -1, pszStr, len, NULL, NULL);
if (wstr) delete[] wstr;
std::string str = pszStr;
delete[] pszStr;
return str;
}