gpt4 book ai didi

c++ - 如何使用 nlohmann::json 将 json 对象转换为 map ?

转载 作者:搜寻专家 更新时间:2023-10-31 00:29:41 28 4
gpt4 key购买 nike

例如,使用 nlohmann::json,我可以做到

map<string, vector<int>> m = { {"a", {1, 2}}, {"b", {2, 3}} };
json j = m;

可是我做不到

m = j;

有什么方法可以使用 nlohmann::json 将 json 对象转换为映射?

最佳答案

nlomann::json 可以将 Json 对象转换为大多数标准的 STL 容器 get<typename BasicJsonType>() const

例子:

// Raw string to json type
auto j = R"(
{
"foo" :
{
"bar" : 1,
"baz" : 2
}
}
)"_json;

// find object and convert to map
std::map<std::string, int> m = j.at("foo").get<std::map<std::string, int>>();
std::cout << m.at("baz") << "\n";
// 2

关于c++ - 如何使用 nlohmann::json 将 json 对象转换为 map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40046853/

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