前言
近期用到了json文件,查资料发现了nlohmann\json,下载网址在这里:
json
一、nlohmann\json是什么?
nlohmann\json是快速生成json格式的方式,使用便捷。
二、使用步骤
1.引入库
将下载好的文件中,文件夹中include内的内容复制到自己工程文件中,然后在代码中
#include "nlohmann/json.hpp"
后续为了方便:
using njson = nlohmann::json
查看nlohmann\json的文档进行编写操作。
1.将json转化为string类型
std::string s = j.dump();
std::cout << j.dump(4) << std::endl;
4表示格式中的空格数
2.读取json格式的文件

3. 创建json,包括json结构中的json array
hv::Json j,jPart1,jPart2;
j["test1"] = 42.2;
j["test2"] = "noting";
j["test3"]["count"] = 1;
jPart1["no1"]=1;
jPart1["no2"] =2;
jPart2["no1"] =1.1;
jPart2["no2"] =2.2;
j["test3"]["nomber"][0]= jPart1;
j["test3"]["nomber"][1]=jPart2;
运行结果为:

可以轻松生成具有json array 的json。
总结
此文章也是一种记录,后续用到再更新
本文介绍如何使用 nlohmannjson 库快速生成 JSON 文件,并提供了将 JSON 转换为 string 类型的方法及如何创建包含 JSON 数组的 JSON 结构示例。

1377

被折叠的 条评论
为什么被折叠?



