C++使用nlohmann/json 转 C++ 结构体

78 篇文章 3 订阅

参考网址:https://icode.best/i/33144046003522

#include "json.hpp"
using namespace std;
using namespace nlohmann;

struct JsonData
{
	string path;
	int age;
};

/*NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(JsonData,
	path,
	age)*/

void from_json(const json& j, JsonData& p)
{
	j.at("path").get_to(p.path);
	j.at("age").get_to(p.age);
}

int main()
{
	vector<int> s;
	s.push_back(1);
	s.push_back(2);
	s.push_back(3);

	int s1 = s.back();
	int s2 = 1;

	const char* jsonStr = R"({"FxData":[{"path":"abc1", "age":12},{"path":"abc2", "age":12},{"path":"abc3", "age":12}], "Audio":"click"})";
	const auto& J = json::parse(jsonStr, nullptr, false);
	if (J.contains("Audio"))
	{
		JsonData* jsonData = new JsonData();
		const basic_json<>& list = J["FxData"];
		bool isArray = list.is_array();
		if (isArray)
		{
			for (size_t i = 0; i < list.size(); i++)
			{
				const auto& item = list[i];
				item.get_to(*jsonData); //这里就是因为定义了from_json,才可以使用get_to的方法。
				string path = "ddd"; //默认值
				string path2;
				int age;
				path2 = item.value("path3", path); //因为没有key=path3的,所以使用path填充path2
				item.at("path").get_to(path);
			}
		}
	}
	return 0;
}

使用宏展开的方式:

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(JsonData,
	path,
	age)

其中的宏定义在json.hpp中:

#define NLOHMANN_JSON_FROM_WITH_DEFAULT(v1) nlohmann_json_t.v1 = nlohmann_json_j.value(#v1, nlohmann_json_t.v1);
/*!
@brief macro
@def NLOHMANN_DEFINE_TYPE_INTRUSIVE
@since version 3.9.0
*/
#define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...)  \
    friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }

#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...)  \
    friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { Type nlohmann_json_default_obj; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }

/*!
@brief macro
@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
@since version 3.9.0
*/
#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...)  \
    inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }

#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...)  \
    inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { /*Type nlohmann_json_default_obj;*/ NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }


nlohmann_json_j.value(#v1, nlohmann_json_t.v1);这里使用json.value的取值方式。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要读取一个 JSON 文件中的结构体数组,我们需要定义一个结构体,并使用 `nlohmann_json` 库提供的 `json::parse()` 函数来解析 JSON 文件。以下是一个示例代码: ```c++ #include <iostream> #include <fstream> #include <vector> #include <nlohmann/json.hpp> using json = nlohmann::json; // 定义一个结构体 struct Person { std::string name; int age; }; int main() { // 打开 JSON 文件 std::ifstream ifs("example.json"); // 读取 JSON 文件 json j = json::parse(ifs); // 解析 JSON 数组 std::vector<Person> persons; for (auto& elem : j["data"]) { Person p; p.name = elem["name"]; p.age = elem["age"]; persons.push_back(p); } // 遍历结构体数组 for (auto& p : persons) { std::cout << "Name: " << p.name << ", Age: " << p.age << std::endl; } return 0; } ``` 在这个示例中,我们首先定义了一个名为 `Person` 的结构体,它包含了一个名为 `name` 的字符串和一个名为 `age` 的整数。然后我们打开了一个名为 `example.json` 的 JSON 文件,使用 `json::parse()` 函数将其解析成一个 `json` 对象。由于这个 JSON 对象中含有一个名为 `data` 的结构体数组,我们可以使用 `j["data"]` 来访问它,并使用 for 循环遍历其中的元素,将每个元素解析为一个结构体,并将其添加到一个 `std::vector<Person>` 中。 最后,我们再次使用 for 循环遍历结构体数组,并将每个结构体的 `name` 和 `age` 输出到控制台上。 需要注意的是,在解析 JSON 数组时,我们需要使用 for 循环遍历其中的元素,并将每个元素解析为一个结构体。在解析时,我们可以使用 `elem["name"]` 和 `elem["age"]` 来访问结构体中的字段。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值