小工具知识网

当前位置: 首页 > 行业知识 > [C++ Json开源库] nlohmann安装与使用

行业知识

[C++ Json开源库] nlohmann安装与使用

2026-11-10 04:52:43 来源:互联网转载

nlohmann简介

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

1、易于集成,仅需一个头文件,无需安装依赖

2、易于使用,可以和STL无缝对接,使用体验近似python中的json

安装

Linux下:

git clone https://github.com/nlohmann/json.git

拉取nlohmann库文件

使用

自己建立一个项目工程文件夹,将include下的molhmann文件夹复制到自己工程下的include文件夹下。

测试工程文件结构:

测试的json文件

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

主文件

#include "nlohmann/json.hpp"#include #include using json = nlohmann::json;int main(int argc,char *argv[]) {json j;// 创建 json 对象    if(argc == 1){    std::ifstream jfile("./test/test.json");        jfile >> j;// 以文件流形式读取 json 文件    }    else if(argc == 2){    std::ifstream jfile(argv[1]);        jfile >> j;// 以文件流形式读取 json 文件    }    else{        std::cout 

上一篇:宝塔面板网页怎么设计?

下一篇:开房查询App背后的风险与犯罪隐患揭秘!