使用nlohmann解析json文件-创新互联

使用 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

成都网站建设、网站设计中从网站色彩、结构布局、栏目设置、关键词群组等细微处着手,突出企业的产品/服务/品牌,帮助企业锁定精准用户,提高在线咨询和转化,使成都网站营销成为有效果、有回报的无锡营销推广。创新互联公司专业成都网站建设十多年了,客户满意度97.8%,欢迎成都创新互联客户联系。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()
{//读取方式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
// }

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


新闻标题:使用nlohmann解析json文件-创新互联
文章起源:http://pwwzsj.com/article/djeoch.html