您的位置:首页 > 编程语言 > C语言/C++

C++解析Json数据

2013-06-20 17:52 471 查看
一.下载开源库Jsoncpp,编译

参见:http://blog.sina.com.cn/s/blog_75f1902801015dx6.html,以示感谢。不过我下载

出来的是0.5.0而非作者所说的0.6.0,不过一样能用。

二.在项目中使用

1.将项目中include文件夹复制到你的工程下,并添加使用:右键项目-》添加-》存在的项-

》选中include中json下的.h文件添加使用;将source下的lib_json同样拷贝到项目中(提一句,

我并没有将lib_json中的文件添加到工程中,仅仅是放在文件夹下,但调试的时候,同样跟了进

去。)

2.添加对库的使用,debug版本为:json_vc71_libmtd.lib,release版本为:

json_vc71_libmt.lib。将库拷贝到你的工程中,vs中右键工程-》属性-》连接-》输入-》添加依

赖库(第一项),将库的名字添加进去。

三.解析

1.以下函数包括获取json数据,将乱码(网页编码为UTF-8,工程为Unicode,获取后显示乱码)

转换为CString,再将CString转为string进行解析。

bool CTestJsonDlg::getSearchJson()

{

wchar_t isonUrl[200] = _T("http://sg1.api.bing.com/qsonhs.aspx?q=%E8%AE%BE

%E8%AE%A1");

CInternetSession CSession;

CHttpFile *cFile;

CString szAllData = _T("");

char sReceived[1025] = {0};

try

{

cFile = (CHttpFile*)CSession.OpenURL(isonUrl);

}

catch (CInternetException* e)

{

cFile = NULL;

e->m_dwContext;

e->Delete();

CSession.Close();

AfxMessageBox(_T("CInternetException"));

return FALSE;

}

//将接收到的UTF-8格式编码的内容转为Unicode

/*

CString strBuf;

while (cFile->ReadString(strBuf))

{

char* pStr = (char*)strBuf.GetBuffer(strBuf.GetLength());

int szBuf = MultiByteToWideChar(CP_UTF8, 0, pStr, -1, NULL, 0);

wchar_t* pBuf = new wchar_t[szBuf * sizeof(wchar_t)];

MultiByteToWideChar(CP_UTF8, 0, pStr, -1, pBuf, szBuf*sizeof

(wchar_t));

szAllData += pBuf;

delete[] pBuf;

pBuf = NULL;

}*/

int nCount = 0;

nCount = cFile->Read((LPTSTR)sReceived, 1024);

do

{

sReceived[nCount] = '\0';

int szBufLen = MultiByteToWideChar(CP_UTF8, 0, sReceived, -1, NULL,

0);

wchar_t* pBuf = new wchar_t[szBufLen * sizeof(wchar_t)];

MultiByteToWideChar(CP_UTF8, 0, sReceived, -1, pBuf, szBufLen*sizeof

(wchar_t));

szAllData += pBuf;

delete[] pBuf;

pBuf = NULL;

memset(sReceived, 0, 1025);

} while ((nCount = cFile->Read((LPTSTR)sReceived, 1024)) > 0);

string str;

//将CString转为string

wchar_t* p = szAllData.GetBuffer(szAllData.GetLength());

//WideCharToMultiByte的运用,计算要转换的个数

DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,p,-1,NULL,0,NULL,FALSE);

//psText为char*的临时数组,作为赋值给std::string的中间变量

char *psText;

psText = new char[dwNum];

//WideCharToMultiByte的再次运用,进行转换

WideCharToMultiByte (CP_OEMCP,NULL,p,-1,psText,dwNum,NULL,FALSE);

//std::string赋值

str = psText; //大功告成

//psText的清除

delete []psText;

psText = NULL;

parseJson(str);

cFile->Close();

delete cFile;

cFile = NULL;

CSession.Close();

return TRUE;

}

2.解析数据(不同json数据肯定不同,只是示例而已)

void parseJson(string strValue)

{

//strValue = "{\"key1\":\"value1\",\"array\":[{\"key2\":\"value2\"},

{\"key2\":\"value3\"},{\"key2\":\"value4\"}]}";

Json::Reader reader;

Json::Value my_value;

if (reader.parse(strValue, my_value) && my_value.isMember("AS"))

{

const Json::Value branch = my_value["AS"];

string str = branch["Query"].asString();

int ir = branch["FullResults"].asInt();

const Json::Value arrayObj1 = branch["Results"];

int size1 = arrayObj1.size();

for (int i = 0; i < size1; ++i)

{

Json::Value v = arrayObj1[i]["Type"];

str = v.asString();

Json::Value arrayObj2 = arrayObj1[i]["Suggests"];

int size2 = arrayObj2.size();

for (int j = 0; j < size2; ++j)

{

str = arrayObj2[j]["Txt"].asString();

str = arrayObj2[j]["Type"].asString();

str = arrayObj2[j]["Sk"].asString();

}

}

}

string str = my_value.toStyledString();

}

3.具体一些网上json数据应用可参见:http://g.kehou.com/t1033317914.html

4.重要,相当重要

你的工程所使用的运行时库必须与编译jsoncpp的相同,即你需要右击你的项目-》属性-》配置属性-》Code Generation-》Runtime Library的选项必须相同。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: