参考链接:Here

什么是nlohman json ?

nlohman json GitHub - nlohmann/json: JSON for Modern C++ 是一个为现代C++(C++11)设计的JSON解析库,主要特点是

  1. 易于集成,仅需一个头文件,无需安装依赖
  2. 易于使用,可以和STL无缝对接,使用体验近似python中的json

Minimal Example

CMakeLists.txt 编写教程:Here

# CMakeLists.txt
cmake_minimum_required(VERSION 3.24)
project(nlohmannJson) set(CMAKE_CXX_STANDARD 17) include_directories("include")
add_executable(${PROJECT_NAME} src/main.cpp)
// main.cpp
#include <iostream>
#include "nlohmann/json.hpp" using namespace std;
using namespace nlohmann; int main() {
auto config_json = json::parse(R"({"Happy": true, "pi": 3.1415})");
cout << config_json << endl; return 0;
}

Advanced Sample

示范用法

nlohman::json 库操作的基本对象是 json Object,全部操作围绕此展开

引入代码

#include <fstream>
#include <nlohmann/json.hpp> using json = nlohmann::json;

读取 JSON 文件

#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json; // ... // std::ifstream f("example.json");
std::ifstream f("../config/example.json")
json data = json::parse(f);

从JSON文本创建JSON对象

假设您要在文件中创建此文本 JSON 值作为对象:

{
"pi": 3.141,
"happy": true
}

有多种选择:

// 用原始字符串和json::parse进行初始化
json ex1 = json::parse(R"(
{
"pi": 3.1415,
"happy": true
}
)"); // 用原始字符串和literals进行初始化
using namespace nlohmann::literals;
json ex2 = R"(
{
"pi": 3.141,
"happy": true
}
)"_json; // 使用初始化列表
json ex3 = {
{"happy", true},
{"pi", 3.141},
};

JSON 作为第一类数据类型

下面是一些示例,可让您了解如何使用该类。

假设您要创建 JSON 对象

{
"pi": 3.141,
"happy": true,
"name": "Koshkaaa",
"nothing": null,
"answer": {
"everything": 42
},
"list": [1, 0, 2],
"object": {
"currency": "USD",
"value": 42.99
}
}

使用此库,您可以编写:

// 创建一个空 json 对象 (null)
json j; // 添加一个 double 类型成员
j["pi"] = 3.141; // 添加一个 bool 类型的成员
j["happy"] = true; // 添加一个字符串类型的成员
j["name"] = "Koshkaaa"; // 添加一个空成员
j["nothing"] = nullptr; // 添加一个对象中的对象成员 {"answer":{"everything":42}}
j["answer"]["everything"] = 42; // 添加一个数组对象, 使用 vector
j["list"] = {1,0,2}; // 添加一个对象,使用初始化列表 {"object":{"currency": "USD","value":42.99}}
j["object"] = { {"currency", "USD"}, {"value", 42.99} }; // 也可以一次性写完
json j2 = {
{"pi", 3.141},
{"happy", true},
{"name", "Koshkaaa"},
{"nothing", nullptr},
{"answer", {
{"everything", 42}
}},
{"list", {1, 0, 2}},
{"object", {
{"currency", "USD"},
{"value", 42.99}
}}
};

请注意,在所有这些情况下,您永远不需要“告诉”编译器要使用哪种 JSON 值类型。如果你想明确或表达一些边缘情况,函数json::array()和json::object()会有所帮助:

// 初始化一个空数组对象
json empty_array_explicit = json::array(); // 初始化一个对象
json empty_object_implicit = json({});
json empty_object_explicit = json::object(); // 初始化数组键值对 [["currency", "USD"], ["value", 42.99]]
json array_not_object = json::array({ {"currency", "USD"}, {"value", 42.99} });

序列化/反序列化

json对象和string互转JSON对象和string互转

您可以通过追加到字符串文本来创建 JSON 值(反序列化):_json

// 从字符串创建json对象
json j = "{ \"happy\": true, \"pi\": 3.141 }"_json;
// 使用原始字符串创建json对象
auto j2 = R"(
{
"happy": true,
"pi": 3.141
}
)"_json;

请注意,如果不附加后缀,则传递的字符串文字不会被解析,而只是用作 JSON 字符串 价值。也就是说,只会存储字符串而不是解析实际对象。_jsonjson j = "{ \"happy\": true, \"pi\": 3.141 }""{ "happy": true, "pi": 3.141 }"

上面的例子也可以用json::parse()显式表示:

auto j3 = json::parse(R"({"happy": true, "pi": 3.141})");

获取 JSON 字符串(序列化):

std::string s = j.dump();    // {"happy":true,"pi":3.141}

// 序列化美化json形式
// 传入数字指定缩进空格数
std::cout << j.dump(4) << std::endl;
// {
// "happy": true,
// "pi": 3.141
// }

流输入输出(例如文件读写、字符串流)

您还可以使用流来序列化和反序列化:

// 从标准输入中反序列化
json j;
std::cin >> j; // 序列化到标准输出
std::cout << j; // 美化输出
std::cout << std::setw(4) << j << std::endl;

文件读写json:std::istream,std::ostream

// 读取json文件
std::ifstream i("file.json");
json j;
i >> j; //json对象美化输出到文件
std::ofstream o("pretty.json");
o << std::setw(4) << j << std::endl;

从迭代器范围读取json

可以从迭代器范围解析 JSON;也就是说,从迭代器可访问的任何容器中,迭代器是 1、2 或 4 个字节的整数类型,将分别解释为 UTF-8、UTF-16 和 UTF-32。例如,std::vector<std::uint8_t>,std::list<std::uint16_t>

std::vector<std::uint8_t> v = {'t', 'r', 'u', 'e'};
json j = json::parse(v.begin(), v.end());
std::vector<std::uint8_t> v = {'t', 'r', 'u', 'e'};
json j = json::parse(v);

自定义数据源

由于 parse 函数接受任意迭代器范围,因此您可以通过实现概念来提供自己的数据源。LegacyInputIterator

struct MyContainer {
void advance();
const char& get_current();
}; struct MyIterator {
using difference_type = std::ptrdiff_t;
using value_type = char;
using pointer = const char*;
using reference = const char&;
using iterator_category = std::input_iterator_tag; MyIterator& operator++() {
MyContainer.advance();
return *this;
} bool operator!=(const MyIterator& rhs) const {
return rhs.target != target;
} reference operator*() const {
return target.get_current();
} MyContainer* target = nullptr;
}; MyIterator begin(MyContainer& tgt) {
return MyIterator{&tgt};
} MyIterator end(const MyContainer&) {
return {};
} void foo() {
MyContainer c;
json j = json::parse(c);
}

类似 STL 的访问

我们将 JSON 类设计为类似于 STL 容器的行为。事实上,它满足可逆容器的要求。

// 用push_back创建一个数组对象
json j;
j.push_back("foo");
j.push_back(1);
j.push_back(true); // 使用 emplace_back
j.emplace_back(1.78); // 迭代访问
for (json::iterator it = j.begin(); it != j.end(); ++it) {
std::cout << *it << '\n';
} // 范围迭代
for (auto& element : j) {
std::cout << element << '\n';
} // getter/setter接口
const auto tmp = j[0].get<std::string>();
j[1] = 42;
bool foo = j.at(2); // 比较
j == R"(["foo", 1, true, 1.78])"_json; // true // 其他
j.size(); // 4 个对象
j.empty(); // 是否为空:false
j.type(); // 获取类型:json::value_t::array
j.clear(); // 清空 // 检查类型
j.is_null();
j.is_boolean();
j.is_number();
j.is_object();
j.is_array();
j.is_string(); // 创建一个对象
json o;
o["foo"] = 23;
o["bar"] = false;
o["baz"] = 3.141; // 使用 emplace
o.emplace("weather", "sunny"); // 迭代访问
for (json::iterator it = o.begin(); it != o.end(); ++it) {
std::cout << it.key() << " : " << it.value() << "\n";
} // 循环访问
for (auto& el : o.items()) {
std::cout << el.key() << " : " << el.value() << "\n";
} // 匿名对象迭代器(C++17)
for (auto& [key, value] : o.items()) {
std::cout << key << " : " << value << "\n";
} // 查找key
if (o.contains("foo")) {
// 找到foo的键值
} // 通过迭代器查找
if (o.find("foo") != o.end()) {
// 找到foo的键值
} // 用count()统计是否有键值
int foo_present = o.count("foo"); // 1
int fob_present = o.count("fob"); // 0 // 删除foo对象
o.erase("foo");

从 STL 容器转换

从STL容器转换到json,std::list转json,std::vector转json

std::vector<int> c_vector {1, 2, 3, 4};
json j_vec(c_vector);
// [1, 2, 3, 4] std::deque<double> c_deque {1.2, 2.3, 3.4, 5.6};
json j_deque(c_deque);
// [1.2, 2.3, 3.4, 5.6] std::list<bool> c_list {true, true, false, true};
json j_list(c_list);
// [true, true, false, true] std::forward_list<int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543};
json j_flist(c_flist);
// [12345678909876, 23456789098765, 34567890987654, 45678909876543] std::array<unsigned long, 4> c_array {{1, 2, 3, 4}};
json j_array(c_array);
// [1, 2, 3, 4] std::set<std::string> c_set {"one", "two", "three", "four", "one"};
json j_set(c_set); // only one entry for "one" is used
// ["four", "one", "three", "two"] std::unordered_set<std::string> c_uset {"one", "two", "three", "four", "one"};
json j_uset(c_uset); // only one entry for "one" is used
// maybe ["two", "three", "four", "one"] std::multiset<std::string> c_mset {"one", "two", "one", "four"};
json j_mset(c_mset); // both entries for "one" are used
// maybe ["one", "two", "one", "four"] std::unordered_multiset<std::string> c_umset {"one", "two", "one", "four"};
json j_umset(c_umset); // both entries for "one" are used
// maybe ["one", "two", "one", "four"]

std::mapjsonstd::unorderedjson

std::map<std::string, int> c_map { {"one", 1}, {"two", 2}, {"three", 3} };
json j_map(c_map);
// {"one": 1, "three": 3, "two": 2 } std::unordered_map<const char*, double> c_umap { {"one", 1.2}, {"two", 2.3}, {"three", 3.4} };
json j_umap(c_umap);
// {"one": 1.2, "two": 2.3, "three": 3.4} std::multimap<std::string, bool> c_mmap { {"one", true}, {"two", true}, {"three", false}, {"three", true} };
json j_mmap(c_mmap); // only one entry for key "three" is used
// maybe {"one": true, "two": true, "three": true} std::unordered_multimap<std::string, bool> c_ummap { {"one", true}, {"two", true}, {"three", false}, {"three", true} };
json j_ummap(c_ummap); // only one entry for key "three" is used
// maybe {"one": true, "two": true, "three": true}

【3rd Party】nlohmann json 基础用法的更多相关文章

  1. json基础用法

    JSON格式 JSON格式(JavaScript Object Notation的缩写)是一种用于数据交换的文本格式,2001年由Douglas Crockford提出,目的是取代繁琐笨重的XML格式 ...

  2. elasticsearch安装与基础用法

    来自官网,版本为2.3 注意elasticsearch依赖jdk,2.3依赖jdk7 下载rpm包并安装 wget -c https://download.elastic.co/elasticsear ...

  3. Docker基础用法篇

    Docker基础用法篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装docker 1>.依赖的基础环境 64 bits CPU Linux Kerner 3.10+ ...

  4. PropertyGrid控件由浅入深(二):基础用法

    目录 PropertyGrid控件由浅入深(一):文章大纲 PropertyGrid控件由浅入深(二):基础用法 控件的外观构成 控件的外观构成如下图所示: PropertyGrid控件包含以下几个要 ...

  5. json基础

    1 xml缺点 用xml表示一个对象,数据存储效率低 <person> <firstName>Morra<firstName> <lastName>Do ...

  6. 再谈Newtonsoft.Json高级用法

    上一篇Newtonsoft.Json高级用法发布以后收到挺多回复的,本篇将分享几点挺有用的知识点和最近项目中用到的一个新点进行说明,做为对上篇文章的补充. 阅读目录 动态改变属性序列化名称 枚举值序列 ...

  7. 【Java EE 学习 31】【JavaScript基础增强】【Ajax基础】【Json基础】

    一.JavaScript基础增强 1.弹窗 (1)使用window对象的showModelDialog方法和showModelessDialog方法分别可以弹出模式窗口和非模式窗口,但是只能在IE中使 ...

  8. logstash安装与基础用法

    若是搭建elk,建议先安装好elasticsearch 来自官网,版本为2.3 wget -c https://download.elastic.co/logstash/logstash/packag ...

  9. BigDecimal最基础用法

    BigDecimal最基础用法 用字符串生成的BigDecimal是不会丢精度的. 简单除法. public class DemoBigDecimal { public static void mai ...

  10. JSON基本用法

    JSON基本用法 2016-08-10 16:42:19   JSON的全称是“JavaScript Object Notation”,意思是JavaScript对象表示法,它是一种基于文本,独立于语 ...

随机推荐

  1. SQL 语言标准简介

    版权声明:原创作品,谢绝转载!否则将追究法律责任. ----- 作者:kirin 一.简介 结构化查询语言(Structured Query Language)简称SQL,是一种特殊目的的编程语言,是 ...

  2. Excel表格数据可视化的六大常见方式,看看你都会吗?

    当涉及到Excel表格数据的可视化,有许多不同的方式可以展示和呈现数据.以下是六种常见的Excel表格数据可视化方式的详细介绍. 1. 条形图(Bar Chart) 条形图是一种常见的数据可视化图表类 ...

  3. C和C++练习

    要点: 1.数组 2.冒泡排序BubbleSort 3.带指针的结构体(malloc,free) 4.字符串操作(拷贝.逆序.比较) 5.格式化输出printf,sprintf 6.格式化输入,sca ...

  4. python 获取apk 信息

    python 获取apk 信息 1.安装androguard pip install androguard 2.获取apk的相关信息 引入基础信息 from androguard.misc impor ...

  5. pytest+allure+jenkins+python+git,实现Linux服务器看测试报告,并发送邮件通知

    1.简介 pytest:一种单元测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,效率更高. allure:是开源测试报告框架. jenkins:持 ...

  6. ElasticSearch之cat pending tasks API

    命令样例如下: curl -X GET "https://localhost:9200/_cat/pending_tasks?v=true&pretty" --cacert ...

  7. node node-sass sass-loader版本兼容问题

    相关概念 sass Sass是一种预处理器脚本语言,可以解释或编译成层叠样式表(CSS). Sass包含两种语法:较旧的语法使用缩进将代码块和换行符分隔为单独的规则:较新的语法SCSS使用像CSS这样 ...

  8. MinIO FTP 断点续传

    MinIO FTP 断点续传 对于minio来说,使用minio官方的Java SDK和开启FTP都是不支持断点续传的.对于要实现http接口的断点续传,可以通过调用Amazon S3 REST AP ...

  9. CSS3学习笔记-句子排版效果

    CSS3提供了丰富的排版效果,可以通过样式属性来控制文本的排列方式.字体样式.行高.字间距等.以下是一些常用的句子排版效果示例: 文本对齐方式: .text-center { text-align: ...

  10. 斯坦福 UE4 C++ ActionRoguelike游戏实例教程 10.5.作业五 为游戏添加一个积分系统,随机生成增益道具

    斯坦福课程 UE4 C++ ActionRoguelike游戏实例教程 0.绪论 概述 本篇文章将解决作业五提出的问题,使用PlayerState,在原本游戏的基础上引入积分系统,实现击杀敌人得分,拾 ...