JSON解析(C++)

本文主要介绍使用 jsoncpp 库,编写C++语言的 JSON 解析程序。

1 概述

jsoncpp 是一个可以与 JSON 进行交互的C++库。官网定义如下:

jsoncpp is an implementation of a JSON reader and writer in C++.

通过使用 jsoncpp ,我们可以对 JSON 进行读写。

2 示例代码

2.1 从字符串中解析json

从字符串中解析 json 的代码(jsonstrparse.cpp)如下:

 
  1. #include <iostream>

  2. #include <string>

  3. #include <jsoncpp/json/json.h>

  4.  
  5. using namespace std;

  6.  
  7. int main()

  8. {

  9. string strJsonContent = "{\"role_id\": 1,\"occupation\": \"paladin\",\"camp\": \"alliance\"}";

  10.  
  11. int nRoleDd = 0;

  12. string strOccupation = "";

  13. string strCamp = "";

  14.  
  15. Json::Reader reader;

  16. Json::Value root;

  17.  
  18. if (reader.parse(strJsonContent, root))

  19. {

  20. nRoleDd = root["role_id"].asInt();

  21. strOccupation = root["occupation"].asString();

  22. strCamp = root["camp"].asString();

  23. }

  24.  
  25. cout << "role_id is: " << nRoleDd << endl;

  26. cout << "occupation is: " << strOccupation << endl;

  27. cout << "camp is: " << strCamp << endl;

  28.  
  29. return 0;

  30. }

  31.  

使用如下命令编译上述代码,命令如下:

g++ -o jsonstrparse jsonstrparse.cpp -ljsoncpp

运行编译生成的程序,结果如下:

从上述结果能够看到,我们成功地解析了字符串中的 json 数据。

2.2 从字符串中解析带有数组的json

示例代码(json_parse_array.cpp)如下:

 
  1. #include <iostream>

  2. #include <string>

  3. #include <jsoncpp/json/json.h>

  4.  
  5. using namespace std;

  6.  
  7. int main()

  8. {

  9. string strJsonContent = "{\"list\" : [{ \"camp\" : \"alliance\",\"occupation\" : \"paladin\",\"role_id\" : 1}, \

  10. {\"camp\" : \"alliance\",\"occupation\" : \"Mage\",\"role_id\" : 2}],\"type\" : \"roles_msg\",\"valid\" : true}";

  11.  
  12. string strType;

  13. int nRoleDd = 0;

  14. string strOccupation;

  15. string strCamp;

  16.  
  17. Json::Reader reader;

  18. Json::Value root;

  19.  
  20. if (reader.parse(strJsonContent, root))

  21. {

  22. // 获取非数组内容

  23. strType = root["type"].asString();

  24. cout << "type is: " << strType << endl;

  25.  
  26. // 获取数组内容

  27. if (root["list"].isArray())

  28. {

  29. int nArraySize = root["list"].size();

  30. for (int i = 0; i < nArraySize; i++)

  31. {

  32. nRoleDd = root["list"][i]["role_id"].asInt();

  33. strOccupation = root["list"][i]["occupation"].asString();

  34. strCamp = root["list"][i]["camp"].asString();

  35.  
  36. cout << "role_id is: " << nRoleDd << endl;

  37. cout << "occupation is: " << strOccupation << endl;

  38. cout << "camp is: " << strCamp << endl;

  39. }

  40. }

  41. }

  42.  
  43. return 0;

  44. }

  45.  
  46.  

编译并运行上述代码,结果如下:

从上述结果能够看到,我们成功地解析了字符串中的 包含数组的 json 数据。

3 使用说明

3.1 编译错误

在上面的源码中,引用 json.h 时使用了如下语句:

#include <jsoncpp/json/json.h>

这里如果使用如下语句替换上面的 include 语句(当然需要同步修改头文件目录),如下:

#include "json.h"

在编译时则会报错,很多错,看起来与 GLIBC 相关的,部分错误信息如下:

 
  1. /usr/include/bits/sched.h:132:20: error: missing binary operator before token "("

  2. # if __GNUC_PREREQ (2, 91)

  3. ^

出现上述编译错误的原因是 jsoncpp 提供的头文件 /usr/local/include/json/features.h 与 GLIBC 提供的头文件 /usr/include/features.h 有冲突,如果我们使用 #include "json.h" 形式包含 json.h,则需要把头文件包含路径设置为 /usr/local/include/json/,此时,如果代码中有地方包含了 features.h(实际上,这是个很常用的头文件,很多地方都会包含此文件),则就会使用 json 提供的 features.h 了(而不是 GLIBC 提供的那个 features.h),从而导致 GLIBC 提供的 features.h 中的一些宏定义缺失(如上面的编译错误),进而导致编译失败。

此问题的解决方案也很简单,就是按照我们提供的示例代码,不直接包含 json.h,而使用间接包含,如:

#include "jsoncpp/json/json.h"

#include "json/json.h"

均可(注意适配修改头文件路径)。

--------------------- 本文来自 liitdar 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/liitdar/article/details/80522415?utm_source=copy

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值