使用 nlohmann 解析 json 文件
创始人
2024-02-20 21:07:27
0

使用 nlohmann 解析 json 文件

  • nlohmann/json的配置
  • json基本数据结构
  • json文件的读取、构造与输出
  • C++对象与nlohmann::json对象的转换
    • C++对象转换成nlohmann::json对象
    • nlohmann::json对象转换成C++对象
  • 序列化
    • 反序列化
    • 序列化

nlohmann 是德国工程师,以其名字为工程名的 nlohmann/json 项目又被成为 JSON for Modern C++

github文件:https://github.com/nlohmann/json#read-json-from-a-file

nlohmann/json的配置

只需在需要使用的文件中#include“json.hpp”

C++含义 .hpp,其实质就是将. cpp的实现代码混入. h头文件当中,定义与实现都包含在同一文件,则该类的调用者只需要include该hpp文件即可,无需再
cpp加入到project中进行编译。

json基本数据结构

在这里插入图片描述
给出相应的json文件

//example.json
{"pi": 3.141,"happy": true,"name": "Niels","nothing": null,"answer": {"everything": 42},"list": [1,0,2],"object": {"currency": "USD","value": 42.99},"add": 10
}

json文件的读取、构造与输出

#include
#include
#includeusing json = nlohmann::ordered_json;//这边为按照json文件的实际内容顺序读取
using namespace std;
void main()
{//读取方式1std::ifstream f("example.json");json data = json::parse(f);//读取方式2std::ifstream i("example.json");json j;i >> j;//构造方式1json j;j["pi"] = 3.141;j["happy"] = true;j["name"] = "Niels";j["nothing"] = nullptr;j["answer"]["everything"] = 42;j["list"] = { 1, 0, 2 };j["object"] = { {"currency", "USD"}, {"value", 42.99} };//构造方式2int num = 42;json j2 = {{"pi", 3.141},{"happy", true},{"name", "Niels"},{"nothing", nullptr},{"answer", {{"everything", num}}},{"list", {1, 0, 2}},{"object", {{"currency", "USD"},{"value", 42.99}}}};j2["add"] = 10;//输出std::ofstream o("pretty.json");o << std::setw(4) << j2 << std::endl;
}

C++对象与nlohmann::json对象的转换

定义结构体

struct Player{string name;int credits;int ranking;
};

C++对象转换成nlohmann::json对象

void to_json(nlohmann::json& j, const Player& p) 
{j = json{  {"name", p.name}, {"credits", p.credits}, {"ranking", p.ranking} };
}

nlohmann::json对象转换成C++对象

void from_json(const nlohmann::json& j, Player& p) 
{j.at("name").get_to(p.name);j.at("credits").get_to(p.credits);j.at("ranking").get_to(p.ranking);
}

序列化

反序列化

通过在字符串尾部追加 _json 来将 string 类型的字面值进行转化:

// create object from string literal
json j = "{ \"happy\": true, \"pi\": 3.141 }"_json;// or even nicer with a raw string literal
auto j2 = R"({"happy": true,"pi": 3.141}
)"_json;如果不添加`_json`, 传递的string类型的数值不会被转化为。当然,也可以直接显式的进行
```c++
// parse explicitly
auto j3 = json::parse(R"({"happy": true, "pi": 3.141})");

序列化

json格式的对象转化为string类型的字符串:

// explicit conversion to string
std::string s = j.dump();    // {"happy":true,"pi":3.141}// serialization with pretty printing
// pass in the amount of spaces to indent
std::cout << j.dump(4) << std::endl;
// {
//     "happy": true,
//     "pi": 3.141
// }

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...