百度云网站开发,免费新建网站,西安免费网站搭建制作,百度文章收录提交入口了解 nlohmann/json 的特点#xff1b;理解编程中 “数据战场”划分的概念#xff1b;迅速上手多种方式构建一个JSON对象#xff1b; 1 特点与安装
nlohmann/json 是一个在 github 长期霸占 “JSON” 热搜版第1的CJSON处理库。它的最大优点是与 C 标准库的容器数据#xf… 了解 nlohmann/json 的特点理解编程中 “数据战场”划分的概念迅速上手多种方式构建一个JSON对象 1 特点与安装
nlohmann/json 是一个在 github 长期霸占 “JSON” 热搜版第1的CJSON处理库。它的最大优点是与 C 标准库的容器数据比如 std::map、std::vector使用体验一致并且搭配良好比如假设 strct T 能与JSON双向互换则 std::vector 自然能与JSON双向互换。
在 msys2 ucrt64 环境下安装命令为
pacman -S mingw-w64-ucrt-x86_64-nlohmann-json如果为了更好地兼容旧 Windows 系统你选择的是 mingw64 环境则该库名称为mingw-w64-x86_64-nlohmann-json。 2 数据战场
一个 C 程序为什么需要使用到 JSON 数据那是因为程序即战场数据即士兵不同的战场需要不同的士兵。下图描述了“数据战场”的概念。 我们在 C 程序中自定义的数据比如一个结构体通常就是该程序中与业务最紧密结合需要参与最多计算的数据因此通常称为 “主战兵”而JSON 数据常用作程序与外部环境的通信格式因此被称为“通信兵”。nlohmann/json 是我们请来的 “雇佣兵”三方库它擅长以 C 结构模拟 JSON 语法从而有效帮助我们的“主战兵”拥有变形金刚的能力……
在 “见证” 主战兵和通信兵如何快速互变之前一定要先清楚二者之间存在一些重要差异
C内置类型体系和JSON的类型体系并非一一对应JSON 不保证字段次序如需要可使用 nlohmann::ordered_json 类
3 视频1快速认识 009-nlohmann/json-1-快速认识 4 Hello JSON
#include cassert#include iostream
#include string
#include vector#include nlohmann/json.hppusing json nlohmann::json;/*{id: ORD20250409-191, //订单号customerID: 10345, //用户IDitems: [123,94320,8], //商品货号列表totalAmount: 172.8, //总价orderDate: 2025/04/09 //下单日期}
*/int main()
{json o1 {{id, ORD20250409-191},{customerID, 10345},{items, {123, 94320, 8}},{totalAmount, 172.8},{orderDate, 2025/04/09}};std::cout o1.dump(2) std::endl;json oArray {123, 94320, 8};std::cout oArray.dump() std::endl;json oInt 123;json oString Tom;json oBoolean true;std::cout int - \t oInt.dump() \n;std::cout string - \t oString.dump() \n;std::cout boolean - \t oBoolean.dump() \n;using namespace nlohmann::literals;// 字符串常量 - json 变量json o2 R({id: ORD20250409-191, customerID: 10345, items: [123,94320,8], totalAmount: 172.8, orderDate: 2025/04/09})_json;assert(o1 o2);std::cout \no2-\n o2.dump(2) std::endl;// 支持注释std::string source R({id: ORD20250409-191, // 订单IDcustomerID: 10345, // 用户IDitems: [123,94320,8], // 包含商品的货号totalAmount: 172.8, orderDate: 2025/04/09});json o3 json::parse(source, nullptr, true, true);assert(o3 o2);std::cout \no3-\n o3.dump(2) std::endl;
}