1.这里只做简单的例子例如json文件内容为
{
"ImagePath": "C:\\Users\\8.mdf",
"DataType": 1,
"IsSelectAll": 0,
"Apps": [
"短信"
],
"InstallAppInfo": [
{
"Name": "日历",
"FirstInstallTime": "2014-11-28T10:12:52",
},
{
"Name": "360云盘",
"FirstInstallTime": "2017-03-01T16:36:13",
},
]
}
2.定义结构体
struct Test
{
string m_strPath;
int m_iDataType;
BOOL m_bSelectedAll;
map<CString,bool> m_mapSelectedApps;
vector<DeviceAppInfo> m_vecAppDetails;
PHLiveParam()
{
m_iDataType = 0;
m_bSelectedAll = TRUE;
m_mapSelectedApps.clear();
}
};
3.解析
BOOL AnalysisPHLiveParam(const CString & strPath, Test& )
{ try
{
CFile file;
if(!file.Open(strPath,CFile::modeRead,NULL))
{
CString strDetailMsg = L"Can not analysis this pram from json file:";
return FALSE;
}
int nSize = file.GetLength();
char *pBuf = new char[nSize + 1];
ZeroMemory(pBuf,nSize + 1);
file.Read(pBuf,nSize);
file.Close();
pBuf[nSize] = 0;
string strData=pBuf;
Json::Reader reader;
Json::Value root;
if(!reader.parse(strData, root, false))
{
CString strDetailMsg = L"Can not analysis json file(Wrong json format ):";
return FALSE;
}
test.m_strPath=root["ImagePath"].asString();
test..m_iDataType=root["DataType"].asInt();
test.m_bSelectedAll = root["IsSelectAll"].asInt();
int n=root["Apps"].size();
string strTmp;
for (int i=0;i<n;i++)
{
strTmp=root["Apps"][i].asString();
test.m_mapSelectedApps[strTmp]=true;}n=root["InstallAppInfo"].size();
for (int i=0;i<n;i++){DeviceAppInfo AppDetailstemp;
AppDetailstemp.m_strAppName =root["InstallAppInfo"][i]["Name"].asString();
AppDetailstemp.m_strAppInstallTime =root["InstallAppInfo"][i]["FirstInstallTime"].asString();
m_vecAppDetails.push_back(AppDetailstemp);
}
return TRUE;
}
catch (std::exception & e)
{
return FALSE;
}
catch (CException* e)
{
return FALSE;
}
}
补充:如果文件格式为 json文件嵌套
例如
{"RegisterSource":{"RegisterBody":"深圳市腾讯计算机系统有限公司"},"bizEntryInfo":[{"title":"王者荣耀","username":"gh_7273a02c7e7e","icon_url":"http://mmbiz.qpic.cn/mmbiz_png/LvM7ibdRGl1tjnzCO6jHT4nZYVI8GV0Hic5c4UxfewBOkHjvK36rrkHpYicEBPaYELHbELcosFHM7jT9ic5cM2MWOw/0?wx_fmt=png"}]}
BOOL AnalysisExternalInfo(const CString &strContent,map<CString,CString> &mapstrInfo)
{
try
{
Json::Reader reader,reader2;
Json::Value root,root2;
if(!reader.parse(ph_convertToUTF8(strContent).c_str(), root, false))
{
/*CString strDetailMsg = L"Can not analysis json file(Wrong json format ):";
strDetailMsg += strPath;*/
//CPHLogUtil::InsertDebugLog("AnalysisPHLiveParam Fail",ph_convertToUTF8(strDetailMsg,CP_ACP1));
return FALSE;
}
root2=root["RegisterSource"];
CString strTmp=ph_convertFromUTF8(root2["RegisterBody"].asString()).c_str();
mapstrInfo.insert(make_pair("RegisterBody",strTmp));
root2=root["BindWxaInfo"];
int n=root2["bizEntryInfo"].size();
for (int i=0;i<n;i++)
{
strTmp=ph_convertFromUTF8(root2["bizEntryInfo"][i]["title"].asString()).c_str();
CString strTitle;
strTitle.Format(L"title%d",i);
mapstrInfo.insert(make_pair(strTitle,strTmp));
strTmp=ph_convertFromUTF8(root2["bizEntryInfo"][i]["username"].asString()).c_str();
mapstrInfo.insert(make_pair("Appusername",strTmp));
}
return TRUE;
}
catch (std::exception & e)
{
//CPHLogUtil::InsertDebugLog("AnalysisPHLiveParam fail",ph_StdExceptionLog(e,""));
return FALSE;
}
catch (CException* e)
{
//CPHLogUtil::InsertDebugLog("AnalysisPHLiveParam fail",ph_ExceptionLog(e));
return FALSE;
}
}