使用 nlohmann 解析 json 文件


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<iostream>
#include<nlohmann/json.hpp>
#include<fstream>

using json = nlohmann::ordered_json;//这边为按照json文件的实际内容顺序读取
using namespace std;
void main()
{
    
    
    //读取方式1
	std::ifstream f("example.json");
	json data = json::parse(f);
	//读取方式2
	std::ifstream i("example.json");
	json j;
	i >> j;
	//构造方式1
	json 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} };

    //构造方式2
	int 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
// }

猜你喜欢

转载自blog.csdn.net/weixin_44478077/article/details/128066213