详细版的Jsoncpp的使用,包括在VS环境下配置


如果想要 Json 部署在 Linux 上
参考: https://blog.csdn.net/2303_76953932/article/details/142703683?spm=1001.2014.3001.5502

C++中原生不支持 Json,所以需要导入 Jsoncpp

准备环境

  1. 下载 Jsoncpp

从 github 上下载源码

1
2
# 这是链接地址
https://github.com/open-source-parsers/jsoncpp

这里我使用的是 git 工具克隆了一份数据下来

1
git clone git@github.com:open-source-parsers/jsoncpp.git

没有 git 工具就点击 code ,然后点击 Download ZIP 下载 ZIP 文件,然后解压到你想要保存的目录下即可

  1. 下载 Cmake 工具
  • 这个工具是为了编译这个 Jsoncpp 源码编译成动态库,然后需要使用的时候直接使用这个库即可
  • 否则需要把这些源码文件和项目文件放一起编译,这样不利于维护,且显得体量过大

VS 环境下配置

  • 由于需要在 VS 上写代码,所以需要将源码编译成 VS 环境下的动态库文件
  • 因此这里演示如何配置

编译

  1. 打开 cmake 工具,然后选择对于的文件路径,第一个路径为下载 Jsoncpp 源码后的路径,第二个路径为编译后文件的路径

QQ_1726993980695

  1. 选择好路径后,点击左下角的 configure 按钮,然后弹出对话框,然后选择对于的 VS 版本,也就是自己本地的版本,我的是 VS2022,就选择了这个,其他选项默认即可

QQ_1726994148628

  1. 点击 Finish 后,等待几秒,点击 Generate 生成 VS 项目,生成后,去对应的路径点击 jsoncpp.sln 打开这个项目

  2. 找到 jsoncpp_lib ,右击选择生成,此时就能得到我们想要的库文件了

使用

  1. 由于需要在编写代码时候需要 jsoncpp 对应的头文件,因此配置头文件,有两种方式
  • 将头文件放到项目目录下,直接被项目包含引用
  • 将头文件放到一个本地固定目录,以后就不再动了,在 VS 项目属性中设置包含这个目录

这里选择第二种

将源码 jsoncpp 中的 include 目录拷贝到本地,注意是源码中的 include 目录,不是生成的 VS 项目中的 include 目录

QQ_1726994990715

由于这个 include 目录下有个 json 目录,并且这个目录中才是存放的头文件,所以访问里面的头文件时需要这样

1
#include <json/json.h>

不建议把头文件移出,这会破坏整个目录结构,存在隐患

  1. 还要将对应的动态库也放在本地固定目录
  • 一个是前面步骤中生成的 vs 项目的 bin 目录下的 Debug 目录中的 jsoncpp.dllQQ_1726995171031

  • 一个是前面步骤中生成的 vs 项目的 lib 目录下的 Debug 目录中的 jsoncpp.libQQ_1726995275746

将这两个文件复制到之前 include 同级目录下,新建一个 lib 目录,放在这里面 QQ_1726995352345

  1. 接下来就是在 VS 中配置了

点击小扳手,打开项目属性 QQ_1726995504028

指定头文件的路径:点击这个,然后编辑包含目录

QQ_1726995576711 1

填写刚刚拷贝 include 的固定目录路径位置

QQ_1726995678575

指定库路径:再编辑库目录

QQ_1726995765142

将刚刚拷贝动态库和导入库到的固定目录路径填写进去

QQ_1726995799658

还需要告诉 VS 需要加载的动态库是哪一个

QQ_1726995872192

填写导入库的名称,也就是 jsoncpp.lib

QQ_1726995914039

到这里就配置完成了,如果执行报错不能找到动态库 jsoncpp.dll 只需要将这个文件拷贝到当前 VS 测试项目代码位置即可

QQ_1726996527484

测试代码:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <json/json.h>
using namespace Json;
using namespace std;
int main()
{
Value root;
root["message"] = "Hello, JsonCpp!";
cout << root.toStyledString() << std::endl;
return 0;
}

基础

概述

  • 是一个完全独立于编程语言的文本格式,用来存储和表示数据的
  • Json 是一种数据格式,和语言无关,在什么语言中都可以使用 Json

用途:

  • 组织数据(数据序列化),用于数据的网络传输
  • 组织数据(数据序列化),写磁盘文件实现数据的持久化存储(一般以. json 作为文件后缀)

Json 中主要有两种数据格式:Json 数组和 Json 对象,并且这两种格式可以交叉嵌套使用

Json 数组

  • Json 数组使用 [] 表示,[] 里边是元素,元素和元素之间使用逗号间隔,最后一个元素后边没有逗号
  • 一个 Json 数组中支持同时存在多种不同类型的成员,包括:整形、 浮点、 字符串、 布尔类型、 json 数组、 json 对象、 空值-null
1
2
3
4
5
[
"zhangsan", 176, 20, false,
["北京", "上海", "天津", "重庆"],
{"Sex": "boy", "father: "Alice"}
]

Json 对象

  • Json 对象使用 {} 来描述,每个 Json 对象中可以存储若干个元素,每一个元素对应一个键值对(key:value 结构),元素和元素之间使用逗号间隔,最后一个元素后边没有逗号
  • 键值(key)必须是字符串,位于同一层级的键值不要重复(因为是通过键值取出对应的 value 值)
  • value 值的类型是可选的,可根据实际需求指定,可用类型包括:整形、 浮点、 字符串、 布尔类型、 json 数组、 json 对象、 空值-null

Jsoncpp 的使用

jsoncpp 库中的类被定义到了一个 Json 命名空间中

1
using namespace Json;

使用 jsoncpp 库解析 json 格式的数据,我们只需要掌握三个类:

  • Value 类:将 json 支持的数据类型进行了包装,最终得到一个 Value 类型
  • FastWriter 类:将 Value 对象中的数据序列化为字符串
  • Reader 类:反序列化,将 json 字符串解析成 Value 类型

Value

1
2
3
4
5
// 判断保存的类型是否是 string / 数组 / 对象
// 这里这列举了三个,还有其他数据类型,如 int, double....
bool isString() const;
bool isArray() const;
bool isObject() const;
1
2
3
4
// 将 Value 对象转换为实际类型
Int asInt() const;
JSONCPP_STRING asString() const;
const char* asCString() const
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 对 Json 数组的操作
ArrayIndex size() const;
Value& operator[](ArrayIndex index);
Value& operator[](int index); // 可以直接下标访问
const Value& operator[](ArrayIndex index) const;
const Value& operator[](int index) const;

// 根据下标的 index 返回这个位置的 value 值
// 如果没找到这个 index 对应的 value, 返回第二个参数d efaultValue
Value get(ArrayIndex index, const Value& defaultValue) const;
Value& append(const Value& value);
const_iterator begin() const;
const_iterator end() const;
iterator begin();
iterator end();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 对 json 对象的操作
Value& operator[](const char* key);
Value& operator[](const JSONCPP_STRING& key);
Value& operator[](const StaticString& key);

const Value& operator[](const char* key) const;
const Value& operator[](const JSONCPP_STRING& key) const;

// 通过key, 得到value值
Value get(const char* key, const Value& defaultValue) const;
Value get(const JSONCPP_STRING& key, const Value& defaultValue) const;
Value get(const CppTL::ConstString& key, const Value& defaultValue) const;

// 得到对象中所有的键值
typedef std::vector<std::string> Members;
Members getMemberNames() const;

FastWriter

1
2
3
4
5
6
// 将 Value 对象数据序列化为 string
// 序列化得到的字符串有样式 -> 带换行 -> 方便阅读 -> 适用于写配置文件的时候
std::string toStyledString() const;

// 将数据序列化 -> 单行 -> 适用于网络传输的时候
std::string Json::FastWriter::write(const Value& root);

Reader

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Reader 类
bool Json::Reader::parse(const std::string& document,
Value& root, bool collectComments = true);
参数:
- document: json格式字符串
- root: 传出参数, 存储了json字符串中解析出的数据
- collectComments: 是否保存json字符串中的注释信息

// 通过begindoc和enddoc指针定位一个json字符串
// 这个字符串可以是完成的json字符串, 也可以是部分json字符串
bool Json::Reader::parse(const char* beginDoc, const char* endDoc,
Value& root, bool collectComments = true);

// write的文件流 -> ofstream
// read的文件流 -> ifstream
// 假设要解析的json数据在磁盘文件中
// is流对象指向一个磁盘文件, 读操作
bool Json::Reader::parse(std::istream& is, Value& root, bool collectComments = true);

示例

准备的 Json 数据

1
2
3
4
5
6
7
8
[
12,
12.34,
true,
"tom",
["jack", "ace", "robin"],
{"sex":"man", "girlfriend":"lucy"}
]

写 Json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
void writeJson()
{
// 将最外层的数组看做一个 Value
// 最外层的 Value 对象创建
Value root;
// Value 有一个参数为 int 行的构造函数
root.append(12); // 参数进行隐式类型转换
root.append(12.34);
root.append(true);
root.append("tom");

// 创建并初始化一个子数组
Value subArray;
subArray.append("jack");
subArray.append("ace");
subArray.append("robin");
root.append(subArray);

// 创建并初始化子对象
Value subObj;
subObj["sex"] = "woman"; // 添加键值对
subObj["girlfriend"] = "lucy";
root.append(subObj);

// 序列化
// 有格式的字符串
string str = root.toStyledString();

// 一整行
// FastWriter f;
// string str = f.write(root);

// 将序列化的字符串写磁盘文件
ofstream ofs("test.json");
ofs << str;
ofs.close();
}

读 Json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
void readJson()
{
// 1. 将磁盘文件中的 json 字符串读到磁盘文件
ifstream ifs("test.json");

// 2. 反序列化 -> value 对象
Value root;
Reader r;
r.parse(ifs, root);

// 3. 从 value 对象中将数据依次读出
if (root.isArray())
{
// 数组, 遍历数组
for (int i = 0; i < root.size(); ++i)
{
// 依次取出各个元素, 类型是 value 类型
Value item = root[i];
// 判断 item 中存储的数据的类型
if (item.isString()){
cout << item.asString() << ", ";
}
else if (item.isInt()){
cout << item.asInt() << ", ";
}
else if (item.isBool()){
cout << item.asBool() << ", ";
}
else if (item.isDouble()){
cout << item.asFloat() << ", ";
}
else if (item.isArray())
{ // 数组
for (int j = 0; j < item.size(); ++j) {
cout << item[j].asString() << ", ";
}
}
else if (item.isObject())
{
// 对象
// 得到所有的key
Value::Members keys = item.getMemberNames();
for (int k = 0; k < keys.size(); ++k) {
cout << keys.at(k) << ":" << item[keys[k]] << ", ";
}
}
}
cout << endl;
}
}

上述读数据如此之繁琐,是因为假设我们完全不知道 Json 数据封装的数据格式 但是实际中,我们其实大多是知道这个数据格式的,也就能直接读取对应数据了


说明:参考学习:https://subingwen.cn/