Nlohmann 的 json 库,json 数组到结构体向量,结构内有指针



我看到了这篇关于将json数组转换为structs向量的文章。我有一个名为Foo:的结构

typedef struct Foo {
Foo* SubFoo;
} Foo;

当我尝试这样做时:

void from_json(const nlohmann::json& j, Foo& f) {
j.at("SubFoo").get_to(f.SubFoo);
}

它给了我这个错误:

error: no matching function for call to 'nlohmann::basic_json<>::get_to(Foo*&) const'
j.at("SubFoo").get_to(a.SubFoo);

那么,如何使用指向值的指针从json中获取呢?

只需取消引用指针:

void from_json(const nlohmann::json& j, Foo& f) {
j.at("SubFoo").get_to(*f.SubFoo);
}

这是一个演示。

不过,您必须确保没有取消引用无效的指针。

最新更新