MFC里面解析json文件格式
CString strTemp; //CString ->string; string stringMsg = (LPCSTR)(CStringA)strTemp; //string -> CString strTemp= stringMsg.c_str();
原本是想接入jsonCPP,不过多线程调试一直不兼容,修改 MDd,MTd还是没有什么用;后来想了想,干脆自己写个;
思路:用字典解决;
void CDlg::SetMap(CMap<CString, LPCTSTR, CString, LPCTSTR> &mapContent,CString msgJson) {
CStringArray strArray;
CString strTemp = msgJson;
string stringMsg = (LPCSTR)(CStringA)strTemp;
strTemp.Remove('"');
strTemp.Remove('{');
strTemp.Remove('}');
int strTempLength = strTemp.GetLength();
int index = 0;
while(true){
index = strTemp.Find(',');
if (-1 == index) {
strArray.Add(strTemp);
break;
}
strArray.Add(strTemp.Left(index));
strTemp = strTemp.Right(strTemp.GetLength() - index - 1);
}
int length = strArray.GetSize();
CString strMap;
int subIndex = 0;
CString strKey("");
CString strValue("");
for (int i = 0; i < length; i++) {
strMap = strArray.GetAt(i);
subIndex = strMap.Find(':');
strKey = strMap.Left(subIndex);
strValue = strMap.Right(strMap.GetLength() - subIndex - 1);
mapContent.SetAt(strKey, strValue);
}
}
void CDlg::MsgJson(CString msgJson) { CMap<CString, LPCTSTR, CString, LPCTSTR> my_Map; SetMap(my_Map, msgJson); CString pLook; my_Map.Lookup(L"type", pLook); };
这样就可以解析简单的json.如果有多层,那么就首先通过寻找 {},进行数据分割,原理大同小异;
posted on 2017-08-19 11:03 maomingchao 阅读(5200) 评论(0) 收藏 举报
浙公网安备 33010602011771号