-
-
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
Keys when iterating over objects #67
Comments
Found one solution, for ( auto &mol : j["moleculelist"].get<json::object_t>() ) { ... } which returns a |
Unfortunately, there is no way for a JSON value to "know" whether it is stored in an object, and if so under which key. All you can do is to use the For your code: for (auto it = j["moleculelist"].begin(); it != j["moleculelist"].end(); ++it)
{
std::cout << it.key() << " | " << it.value() << "\n";
} (Here,
I hope this helps! |
This will work nicely, thanks! |
for ( auto it: j["moleculelist"].items() )
{
std::cout << it.key() << " | " << it.value() << "\n";
} |
for (auto& [key, value] : j["moleculelist"].items())
{
std::cout << key << " | " << value << std::endl;
} |
@Tudyx FYI, |
So I'm new to this json library, and I just wanted to let you know that It has taken me an hour to figure out how to squeeze the key names out of the iterator. It is highly opaque and non-intuitive, because all of the examples depict a scenario where the coder knows the names of all the keys (this includes examples in the doxygen, which is hard to find). It is not an obscure use case for the coder to have no idea what keys may exist in some json file. Can you please make some very crisp examples such as those given in this issue, and put them in the front page example sections? Just a suggestion, thanks. |
Thanks for the feedback. Indeed, iterators and in particular the |
Actually I did find that page and it didn't help me very much because it doesn't explicitly state the scenarios from which the key is accessible. The thing is that I never, ever use |
Thanks for the feedback. The doxygen documentation is indeed not as nice as it could be. Would https://json.nlohmann.me/api/basic_json/items/ have been more helpful? It's unfortunately not linked on https://json.nlohmann.me/features/iterators/. |
Yes, the |
I have trying to parse json, but when I use key property it returns "0". myTest.json file contains following {
URLs: [
{
"Key1":
{
"id" : "myid1",
"status" : "online"
}
},
{
"Key2":
{
"id" : "myid2",
"status" : "online"
}
}
]
} nlohmann::json myJson;
//myTest.json is parsed using and myJson has the file content. This is working fine
if(myJson.count("URLs"))
{
int numOfURLs = myJson["URLs"].size();
//numOfURLs is correct and returns 2
for (auto itr = responseJson["URLs"].begin(); itr != responseJson["URLs"].end(); ++itr)
{
std::string sURLKey = itr.key();
//sURLKey = 0, expecting it to be key1 or key2
auto URLJson = itr.value();
std::string sId = URLJson["id"];
std::string sStatus = URLJson["status"];
}
} |
The JSON value at From https://json.nlohmann.me/api/basic_json/items/#notes:
|
This actually doesn't work (for me at least). Would have loved to use structured bindings but my values are strings and the value ends up being an integer with structured bindings in my code. |
Without more information other than "doesn't work", it's hard to help. What is the content of |
I gave all the info I have. ie. my data is strings but structured bindings give integers. I'm of course talking of the types not anything else because why would I look at a string through the lens of an integer type edit: perhaps this will be instructive though, my json data this was dealing with for me looked like "Hardlinks": {
"/by-study-date/": "0008,0020",
"/by-pid/": "0010,0020",
"/by-dob/": "0010,0030"
}, not really sure how though since this would only be known at runtime |
How are you determining that it's an integer? There is nothing in that code sample that is checking the type. Do you have more code somewhere? |
I must admit I'm relying on the IDE(clion) for this as I haven't compiled with code to check the types. It tells me it's an unsigned long edit: so I went back and reimplemented the structured bindings in a unit test.. The IDE was telling me a false type, seems to work just fine. Even threw exceptions if I tried to use it as the wrong type. That's my bad! |
Thanks for confirming. The type of |
I'm trying to iterate over a json input file containing an unknown number of molecules (see below). For each of these the key is the name which I currently have found no way to extract. Is this possible?
Code:
The text was updated successfully, but these errors were encountered: