解析json文件
来源:互联网 发布:社会化网络的意义 编辑:程序博客网 时间:2024/06/14 16:02
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;}}
阅读全文
0 0
- JSON库解析json文件
- Json解析本地json文件
- JSon文件解析
- ajax解析json文件
- 转:解析json文件
- JSon文件解析
- Json文件解析
- 解析本地json文件
- java解析json文件
- JSon文件解析
- 解析json文件
- js解析json文件
- json数据源文件解析
- 从文件解析json
- 解析json文件
- python解析json文件
- 解析Json文件
- python解析json文件
- 微信小程序二维码如何生成?
- ubuntu16.0.4设置固定ip
- 内联函数及4种显示转化
- 安卓学习——四大组建之Activity生命周期
- TraceView工具(Device Monitor)介绍
- 解析json文件
- [POJ 1279]Art Gallery:半平面交
- Apache ActiveMQ实战(2)-集群
- 『ORACLE』 PLSQL删除数据示例(11g)
- Ceph修改OSD和Monitor的网络
- mysql提高(索引二十四)
- SpringBoot之他乡遇故知
- 窗口遮盖检测
- centos 7深度学习环境部署