For example, with nlohmann::json, I can do
map<string, vector<int>> m = { {"a", {1, 2}}, {"b", {2, 3}} };
json j = m;
But I cannot do
m = j;
Any way to convert a json object to a map with nlohmann::json?
nlomann::json can convert Json objects to to most standard STL containers with get<typename BasicJsonType>() const
Example:
// 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With