如果想要 Json 部署在 Linux 上
参考: https://blog.csdn.net/2303_76953932/article/details/142703683?spm=1001.2014.3001.5502
C++中原生不支持 Json
,所以需要导入 Jsoncpp
库
准备环境
- 下载
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 文件,然后解压到你想要保存的目录下即可
- 下载 Cmake 工具
- 这个工具是为了编译这个 Jsoncpp 源码编译成动态库,然后需要使用的时候直接使用这个库即可
- 否则需要把这些源码文件和项目文件放一起编译,这样不利于维护,且显得体量过大
VS 环境下配置
- 由于需要在 VS 上写代码,所以需要将源码编译成 VS 环境下的动态库文件
- 因此这里演示如何配置
编译
- 打开 cmake 工具,然后选择对于的文件路径,第一个路径为下载 Jsoncpp 源码后的路径,第二个路径为编译后文件的路径

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

点击 Finish
后,等待几秒,点击 Generate
生成 VS 项目,生成后,去对应的路径点击 jsoncpp.sln
打开这个项目
找到 jsoncpp_lib
,右击选择生成,此时就能得到我们想要的库文件了
使用
- 由于需要在编写代码时候需要
jsoncpp
对应的头文件,因此配置头文件,有两种方式
- 将头文件放到项目目录下,直接被项目包含引用
- 将头文件放到一个本地固定目录,以后就不再动了,在 VS 项目属性中设置包含这个目录
这里选择第二种
将源码 jsoncpp
中的 include
目录拷贝到本地,注意是源码中的 include
目录,不是生成的 VS 项目中的 include
目录

由于这个 include
目录下有个 json
目录,并且这个目录中才是存放的头文件,所以访问里面的头文件时需要这样
不建议把头文件移出,这会破坏整个目录结构,存在隐患
- 还要将对应的动态库也放在本地固定目录
将这两个文件复制到之前 include
同级目录下,新建一个 lib
目录,放在这里面 
- 接下来就是在 VS 中配置了
点击小扳手,打开项目属性 
指定头文件的路径:点击这个,然后编辑包含目录

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

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

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

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

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

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

测试代码:
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 命名空间中
使用 jsoncpp
库解析 json
格式的数据,我们只需要掌握三个类:
Value
类:将 json
支持的数据类型进行了包装,最终得到一个 Value
类型
FastWriter
类:将 Value
对象中的数据序列化为字符串
Reader
类:反序列化,将 json
字符串解析成 Value
类型
Value
1 2 3 4 5
|
bool isString() const; bool isArray() const; bool isObject() const;
|
1 2 3 4
| 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
| ArrayIndex size() const; Value& operator[](ArrayIndex index); Value& operator[](int index); const Value& operator[](ArrayIndex index) const; const Value& operator[](int index) const;
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
| 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;
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
|
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
| bool Json::Reader::parse(const std::string& document, Value& root, bool collectComments = true); 参数: - document: json格式字符串 - root: 传出参数, 存储了json字符串中解析出的数据 - collectComments: 是否保存json字符串中的注释信息
bool Json::Reader::parse(const char* beginDoc, const char* endDoc, Value& root, bool collectComments = true);
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 root; 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();
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() { ifstream ifs("test.json"); Value root; Reader r; r.parse(ifs, root);
if (root.isArray()) { for (int i = 0; i < root.size(); ++i) { Value item = root[i]; 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()) { 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/


xingzhu
keep trying!keep doing!believe in yourself!
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 星竹!