MFC JSON解析-开源jsoncpp

来源:互联网 发布:c语言头文件作用 编辑:程序博客网 时间:2024/05/17 00:06
前一章说通过百度api获取身份证信息(通过身份证获取性别,地址和出生年月等信息),但是获取到的数据是一个json字符串。如:
    {"errNum":0,"retMsg":"success","retData":{"address":"\u56db\u5ddd\u7701\u5185\u6c5f\u5e02\u5a01\u8fdc\u53bf","sex":"M","birthday":"1990-12-26"}}

我们需要从中提取出性别,地址和出生年月等信息。这就需要json解析,我用的是开源jsoncpp库。
1. 下载地址:http://sourceforge.net/projects/jsoncpp/ 或者https://github.com/open-source-parsers/jsoncpp ;下载后解压。
2. 有两种使用方法,一种是自己编译成lib静态库,另外一种是直接使用源代码。
自己编译是在jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\makefiles\vs71目录,VS打开编译即可。
但是我编译后使用发现错误,LNK2038 RuntimeLibrary 不匹配什么的,还有库与微软默认库冲突,后来还是用的源代码。(注意事项:1.那个版本的VS编译的库就需要哪个版本的VS使用,其他版本使用会出错;2.C/C++-代码生成-运行库 这里选项需要注意,编译时什么选择,使用也应该是一样的选择;)
3. 源码有两个目录需要拷贝,一个是jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\src\lib_json,直接拷贝文件到工程下即可,另外一个是 \jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\include\json下文件,此时注意要文件夹json一起拷贝(因为他

include json/value.h

)。可能需要改为:#include “json/value.h”。

4.  使用代码#include <afxinet.h> #include "json.h"void CTestJsonDlg::OnBnClickedButton1(){    //使用百度api获取数据-----------------------------------------------------------------    char buf[1000] = { 0 };    CString StrDate, strValue;    CInternetSession session;    CHttpFile* file = NULL; int nRead;    file = (CHttpFile *)session.OpenURL(_T("http://apistore.baidu.com/microservice/icardinfo?id=xxxxxxxxxxxxxx"));    if (NULL != file){        //Do something here with the web request        //Clean up the file here to avoid a memory leak!!!!!!!              while ((nRead = file->Read(buf, sizeof(buf))) > 0){            StrDate = buf;        }        GetDlgItem(IDC_ID_CARD)->SetWindowText(StrDate);        file->Close();        delete file;    }    session.Close();    //解析json数据-----------------------------------------------------------------------       CString strTemp;    std::string value;    Json::Reader reader;    Json::Value root;    if (reader.parse(buf, root))  // reader将Json字符串解析到root,root将包含Json里所有子元素      {        int errNum = root["errNum"].asInt();// 访问节点,errNum = 0              std::string retMsg = root["retMsg"].asString();  // 访问节点,retMsg = "success"                 strTemp = retMsg.c_str();        MessageBox(strTemp);        Json::Value list = root["retData"]; //包含addresss,sex,birthday            if (!list.isNull()){    //      int count = list.size();                        Json::Value::Members members(list.getMemberNames());        //      std::sort(members.begin(), members.end());            //解析后面的嵌套json搞了半天,最后才在测试代码中发现解决方案。            for (auto it = members.begin(); it != members.end();++it)            {                const std::string &name = *it;                std::string str = list[name].asCString();            //  strTemp = str.c_str();                strTemp = UTF8ToUnicode(const_cast<char *>(str.c_str()));                MessageBox(strTemp);            }        }    }}

一个需要注意的地方是编码问题,由于地址是中文,所以需要进行UTF8ToUnicode转码。还有添加的头文件需要去掉添加预处理头选项,或者#include “StdAfx.h”。
Error:
1.json_value.asm”: No such file or directory
2.jsoncpp fatal error link1257
编译库文件时,配置属性-常规-全程序优化 无全程序优化
C/C++ -输出文件 - 汇编程序输出:无列表

3 0