nlohmann/json是一个用于解析json的开源C++库,口碑一流,号称有业界最好的性能,并且使用非常方便直观,是很多c++程序员的首选。

#include "nlohmann/json.hpp"

1. 输入输出

1.1. 输入

1.1.1. 从流输入

nlohmann::json json_data;
std::ifstream(can_file_path.string()) >> json_data;
nlohmann::json front_left_wheel_speed_data = json_data["key"];

1.1.2. 从文件输入

std::ifstream ifs(json_path);
nlohmann::json json = json::parse(ifs);

1.1.3. 从nlohmann::json对象输入

#include "nlohmann/json.hpp"nlohmann::json json;int value = json["value"]// 读取字典
std::string name, credits, ranking;
json.at("name").get_to(name);
json.at("credits").get_to(credits);
json.at("ranking").get_to(ranking);// 读取数组
nlohmann::json data = commands["Data"];
std::cout << "Number of items in Data: " << data.size() << std::endl;
for (auto it = data.begin(); it != data.end(); ++it) {std::cout << it.key() << ": " << it.value() << std::endl;
}std::string GetString(const nlohmann::json &json, const std::string &key) {const auto iter = json.find(key);if (iter == json.end()) {std::cerr << "The json has no such key: " << key << std::endl;}if (!iter->is_string()) {std::cerr << "The value of json[" << key << "] is not a string" << std::endl;}return *iter;
}

1.2. 输出

std::ofstream("rankings.json") << j;

2. 解析与序列化

2.1. 从字符串解析

std::string s = R"({"name": "Judd Trump","credits": 1754500,"ranking": 1
})";auto j = json::parse(s);

2.3. 序列化为字符串

std::string s = j.dump();

3. 声明与构造

3.1. 纯粹声明

json j1;
json j2 = json::object();
json j3 = json::array();std::cout << j1.type_name() << std::endl; // output: null
std::cout << j2.type_name() << std::endl; // output: object
std::cout << j3.type_name() << std::endl; // output: array

3.2. 内容构造

json j = R"({"name": "Judd Trump","credits": 1754500,"ranking": 1
})"_json;json j{{ "name", "Judd Trump"},{ "credits", 1754500 },{ "ranking", 1}
};

参考文献

nlohmann/json 的主要用法 - 麦仲肥 - 博客园

c – 如何从nlohmann json获取数组 - 程序园

nlohmann/json使用笔记相关推荐

  1. C++ nlohmann/json 的主要用法

    文章目录 前言 准备工作 引用头文件: 主要用法: 一.声明与构造 1 纯粹声明 2 内容构造 二.解析与序列化 字符串 解析 序列化 文件:// 比如有文件 c:\rankings.json,其内容 ...

  2. nlohmann json用法

    nlohmann json用法 介绍 demo 介绍 一个开源的c++ json库,类似于STL,挺好用的 demo nlohmann::json j; j["x"] = 1; j ...

  3. c++11:nlohmann::json进阶使用(三)使用basic_json模板类

    nlohmann::json是非常好用的一个json开源解析库.nlohmann/json的源码是基于C++11标准写的,整个源码就是一个文件 nlohmann/json.hpp,引用非常方便. 关于 ...

  4. nlohmann/json 的用法示例

    nlohmann/json 是一个C++实现的JSON解析器,使用非常方便直观.由于查看文档篇幅过长,不便于迅速阅读抓重点.而且,所举例的某些用法实践上其实比较少用到,而某些实践上常用到的一些用法,官 ...

  5. C++ 之 nlohmann::json 一个不错的json库

    前言 最近在搞一个C++项目,用到了json和类的相互转化.但是c++没有反射,也没有像java一个方便的插件,没法办只能自己搞一个了.网上找了一下,发现nlohmann::json不错.已经运用到实 ...

  6. 【C++】C++库nlohmann / json的使用

    nlohmann / json for Modern C++ 前言 一.nlohmann/json库简述 1. 概述 2. 优点 3. 配置 二.nlohmann/json库的基本操作 1. 读取 / ...

  7. c++中nlohmann json的使用

    文章目录 一.json.hpp库下载及安装 1.1 开源地址及引入方法 1.2 demo程序测试 二.nlohmann json基本操作 2.1 由basic value创建json 2.2 由jso ...

  8. C++ nlohmann/json 的主要用法(cout、printf打印,异常try...catch处理)

    文章目录 前言 准备工作 引用头文件: 主要用法: 一.声明与构造 1 纯粹声明 2 内容构造 二.解析与序列化 字符串 解析 序列化 文件:// 比如有文件 c:\rankings.json,其内容 ...

  9. Android Retrofit2 Post请求添加Json类型参数笔记

    Android Retrofit2 Post请求添加Json类型参数笔记 一.添加Header 1.添加单独Header 对于某个API所需要添加Header时,可以直接在Service接口上添加@H ...

最新文章

  1. 百度危矣:乱评程苓峰《360的章鱼手要抢谁家饭碗?》
  2. python具有一些突出优点_Python具有一些突出优点,它们是:()
  3. windows连接投影仪后桌面画面和白板画面不一致
  4. oracle 01013 02063,Oracle11g dblink用户密码大写限制-ORA-02063: preceding line from FOR244_DBLINK...
  5. IOS – OpenGL ES 调节图像阴影 GPUImageHighlightShadowFilter
  6. Linux安装使用redis
  7. WindowsBatch与LinuxShell比较[变量符号和关键字]
  8. SQLSERVER误删除了Windows登录用户验证方式使用Windows身份验证的解决方法
  9. CCF信息学竞赛和教育部竞赛管理出锅重播
  10. android系统中wifi省电模式下的四个基本概念:TIM、DTIM、Beacon-Interval、Listen-Interval
  11. 学3D建模需要有美术功底吗?
  12. python携程怎么做数据同步_python协程中同步如何使用?
  13. modelsim 常用快捷键
  14. S700K表示电路速查【铁路信号技术专栏】转自微信公众号铁路信号技术交流
  15. Flash 多人在线游戏教程 - TicTacToe
  16. 计算机软考高级证自明评职称,IT领域唯一的国家级证书,好处多多,入手不亏...
  17. 2017“编程之美”终章:AI之战勇者为王
  18. python读取tif文件与png文件
  19. 关于office2016卸载后注册表残留无法重新安装问题解决
  20. Stlink固件更新问题“ST-Link is not in the dfu mode Please restart it“的解决方法

热门文章

  1. MySQL基础:数据类型
  2. mysql之修改表引擎
  3. php 所有魔术方法,PHP常用的魔术方法及规则
  4. proxy实现 mysql 读写分离
  5. java中next的用法_关于java iterator的next()方法的用法
  6. (C++)1041 考试座位号
  7. STARTUP报错:ORA-00205: error in identifying control file, check alert log for more info
  8. 学Java需要下载什么软件?都有什么作用?
  9. 上传大文件,出现: 413 request Entity too Large错误的解决办法
  10. post请求中的序列化