C++ JSON解析库:jsoncpp、rapidjson

{“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静态库,另外一种是直接使用源代码

自己编译是在/makefiles/vs71目录,VS打开编译即可。

但是我编译后使用发现错误,LNK2038 RuntimeLibrary 不匹配什么的,还有库与微软默认库冲突,后来还是用的源代码。

注意事项:

1.如果自己编译dll,那个版本的VS编译的库就需要哪个版本的VS使用,其他版本使用会出错;

2.C/C++-代码生成-运行库 这里选项需要注意,编译时什么选择,使用也应该是一样的选择;

3. 直接源码有两个目录需要拷贝,一个是/src/lib_json,直接拷贝文件到工程下即可,另外一个是/include/json下文件,此时注意要文件夹json一起拷贝。

/#include json/value.h

可能需要改为:

#include “json/value.h”

工程目录结构:

0

jsoncpp的include路径为:

0

jsoncpp官方文档:

http://open-source-parsers.github.io/jsoncpp-docs/doxygen/index.html

MFC调用示例:(最好自己参看官方文档,示例不一定适用)

#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); } } } }

5 注意事项:

一个需要注意的地方是编码问题,由于地址是中文,所以需要进行UTF8ToUnicode转码。

还有添加的头文件需要去掉添加预处理头选项,或者#include “StdAfx.h”。

其他Error:

  1. json_value.asm”: No such file or directory
  2. jsoncpp fatal error link1257

编译库文件时,配置属性-常规-全程序优化 无全程序优化。

C/C++ -输出文件 – 汇编程序输出:无列表。

即可。

https://blog.csdn.net/a379039233/article/details/49424393

—————————————————-

https://www.jianshu.com/p/017c13a65622

以前VC解析JSON用的是第三方库JSONCpp,但是JSONCpp对我现在要做的项目来说有缺陷,不支持Unicode的CString解析,要经过繁琐的转化,网上也缺少用的信息来解决这个问题。现在用RapidJson试着来解决这个问题。

简介

RapidJson腾讯推出的轻量级的高效的 C++ JSON 解析/生成器。说他高效肯定有些人不服,那么有多高效呢?这里有他的作者对 28 个 C/C++ JSON 库的比较,即便是老牌的JsonCpp库在效率上依然跟他差了一个量级。更让我看重的是他下面这个优点

http://rapidjson.org/zh-cn/

0

https://github.com/Tencent/rapidjson

RapidJSON 是一个 C++ 的 JSON 解析器及生成器。它的灵感来自 RapidXml

  • RapidJSON 小而全。它同时支持 SAX 和 DOM 风格的 API。SAX 解析器只有约 500 行代码。
  • RapidJSON 快。它的性能可与 

strlen() 相比。可支持 SSE2/SSE4.2 加速。

  • RapidJSON 独立。它不依赖于 BOOST 等外部库。它甚至不依赖于 STL。
  • RapidJSON 对内存友好。在大部分 32/64 位机器上,每个 JSON 值只占 16 字节(除字符串外)。它预设使用一个快速的内存分配器,令分析器可以紧凑地分配内存。
  • RapidJSON 对 Unicode 友好。它支持 UTF-8、UTF-16、UTF-32 (大端序/小端序),并内部支持这些编码的检测、校验及转码。例如,RapidJSON 可以在分析一个 UTF-8 文件至 DOM 时,把当中的 JSON 字符串转码至 UTF-16。它也支持代理对(surrogate pair)及 “\u0000″(空字符)。

在 这里 可读取更多特点。

JSON(JavaScript Object Notation)是一个轻量的数据交换格式。RapidJSON 应该完全遵从 RFC7159/ECMA-404,并支持可选的放宽语法。 关于 JSON 的更多信息可参考:

安装

RapidJSON 是只有头文件的 C++ 库。只需把 include/rapidjson 目录复制至系统或项目的 include 目录中(VS2022需要在项目属性自行添加include目录)

使用示例:

https://github.com/Tencent/rapidjson/#usage-at-a-glance

https://github.com/Tencent/rapidjson/#usage-at-a-glance

您可能还喜欢...

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注