-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
How to check if key existed #1000
Comments
Just like for a map: |
Hi thanks for the reply but im using iterator any idea why is causing this problem ?
|
I would need to see a code example for that. |
When you iterate over an object (which I assume - I have not seen the JSON), doing this with a range for only gives you access to the values, not to the keys. You need to use The exception comes, because you try to assign a number (from |
Then use find: if (json_field_data[p]["reactions"]["summary"].find("total_count") != json_field_data[p]["reactions"]["summary"].end())
// now you can be sure json_field_data[p]["reactions"]["summary"]["total_count"] exists Alternatively, you could use totallike = json_field_data[p]["reactions"]["summary"].value("total_count", 0); |
Thanks for the library and the help |
In case if we are not sure if the keys before the nested key exist, should we use a nested if ? Because in the documentation it's wrote that is an undefined behavior if the element with key idx accessing by if (json_field_data[p].find("reactions") != json_field_data[p].end()){
if(json_field_data[p]["reactions"].find("summary") != json_field_data[p]["reactions"].end()){
if(json_field_data[p]["reactions"]["summary"].find("total_count") != json_field_data[p]["reactions"]["summary"].end())
}
} |
Yes, you need to check for each level. Note there is a Also note that that JSON Pointer could help you, see #2585 (comment) |
Thank you! I chose a JSON Pointer option like in this comment #1449 (comment) if( json_field_data[p].contains(json::json_pointer("/reactions/summary/total_count")) == true ){
} |
Hi to all is there a method to check if the key exist ,
what is the close to
Java JSON
Thanks
The text was updated successfully, but these errors were encountered: