gpt4 book ai didi

c++ - 使用 nlohmann json 包从 json 转换时出错

转载 作者:行者123 更新时间:2023-12-01 14:48:02 27 4
gpt4 key购买 nike

我正在尝试转换表单的 json

{
"content": {
"test_key": "test"
},
"sender": "alice",
"type": "key_type"
}

我的对象是
template<class Content>
struct Event
{
Content content;
std::string type;
};

正在使用模板,因为内容的结构不固定。当我尝试使用 from_json 就像
template<class Content>
void
from_json(const nlohmann::json &obj, Event<Content> &event)
{
event.content = obj.at("content").get<Content>();
event.type = obj.at("type").get<std::string>();
}

我收到错误

[json.exception.out_of_range.403] key 'content' not found



虽然json中有内容键。为什么会这样?
#include <iostream>
#include <nlohmann/json.hpp>

using json = nlohmann::json;
using namespace std;

template<typename Content>
struct Event
{
Content content;
string type;
};

template<typename Content>
void from_json(const nlohmann::json &obj, Event<Content> &event)
{
event.content = obj.at("content").get<Content>();
event.type = obj.at("type").get<string>();
}

struct Key{
string test_key;
string random_data;
};

int main(){
json j={{"content",{{"test_key","test"}}},{"sender","alice"},{"type","key_type"}};

Event<Key> event_instance;

try{
from_json(j,event_instance);
}
catch(json::exception& e){
cout<<e.what()<<endl;
}
}

上面的代码是一个最小的可复制示例

最佳答案

缺少的是对您的类型的序列化程序支持 Key .添加后,提取工作:

void from_json(const nlohmann::json& obj, Key& k) {
k.test_key = obj.at("test_key").get<std::string>();
// k.random_data missing in json
}

template<typename Content>
void from_json(const nlohmann::json& obj, Event<Content>& event) {
event.content = obj.at("content").get<Content>();
event.type = obj.at("type").get<std::string>();
}

Demo

处理可选字段,如 random_data在您的 Key ,你可以创建一个辅助函数,这里叫做 get_optional返回 C++17 std::optional<T> .对于较早的 C++ 版本,您可以使用 boost::optional .
#include <nlohmann/json.hpp>

#include <iostream>
#include <optional>
#include <string>

using json = nlohmann::json;

template<typename Content>
struct Event {
Content content{};
std::string type{};
};

struct Key {
std::string test_key{};
std::optional<std::string> random_data{}; // optional field made optional
};

template<typename T>
std::optional<T> get_optional(const json& obj, const std::string& key) try {
return obj.at(key).get<T>();
} catch(const json::exception&) {
return std::nullopt;
}

void from_json(const json& obj, Key& k) {
k.test_key = obj.at("test_key").get<std::string>();
k.random_data = get_optional<std::string>(obj, "random_data");
}

template<typename Content>
void from_json(const json& obj, Event<Content>& event) {
event.content = obj.at("content").get<Content>();
event.type = obj.at("type").get<std::string>();
}

int main() {
json j = {{"content", {{"test_key", "test"}}},
{"sender", "alice"},
{"type", "key_type"}};

try {
auto event_instance = j.get<Event<Key>>();
std::cout << event_instance.content.test_key << '\n';

if(event_instance.content.random_data) {
std::cout << event_instance.content.random_data.value() << '\n';
} else {
std::cout << "no random_data\n";
}

std::cout << event_instance.type << '\n';
} catch(const json::exception& e) {
std::cerr << e.what() << std::endl;
}
}

Demo

关于c++ - 使用 nlohmann json 包从 json 转换时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61483346/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com