-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
accessing json object as multimap #1892
Comments
The value you posted is not valid JSON. Could you please double check? |
Is
@Prati369, is this right? |
Possibly relevant to #1141. You can std::cout << json_object["2"]["a"]["b"]["c"]["d"]["e"]["f"] or use JSON pointers std::cout << json_object["/2/a/b/c/d/e/f"_json_pointer] If the object hierarchy is not known beforehand, you can traverse the JSON object and look for the key. The following function is one simple way of doing that. Here is the working example: https://wandbox.org/permlink/Hlg3KIpxTSCdSPM8 nlohmann::json find_child_recursive(const nlohmann::json& j, std::string query_key)
{
if(j.is_object())
{
for(const auto& [key, value]: j.items())
{
if(key == query_key)
{
return value;
}
else if(value.is_object() || value.is_array())
{
return find_child_recursive(value, query_key);
}
}
}
else if(j.is_array())
{
for(const auto& item: j)
{
return find_child_recursive(item, query_key);
}
}
return nullptr;
} |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I have a json file in this format. I want to access it . Ex: for key 'f' output should be 1. I tried many ways but unable to access it directly.
JSON
file:{
"1":
"a", "2": [{ "a": [{ "b": [{ "c": [{ "d": [{ "e": { "f": "1", "g": "2" }, "h": [{ "i": "1", "j": "2" }], "j": { "l": "1", "m": 2 }, "n": { "o": "1", "p": 2 } }] }], }] }] } }
The text was updated successfully, but these errors were encountered: