Skip to content
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

A way to get to a JSON values "key" #1258

Closed
robiwano opened this issue Sep 26, 2018 · 5 comments
Closed

A way to get to a JSON values "key" #1258

robiwano opened this issue Sep 26, 2018 · 5 comments

Comments

@robiwano
Copy link

  • Describe the feature in as much detail as possible.

A possibility to get the key of a value, as I haven't been able to find a way to get to it.

  • Include sample usage where appropriate.

For a JSON object obj as such:

auto obj = R"(
  {
    "first": 0,
    "second": true,
    "third": "stringy"
  }
)"_json;

I would like to iterate over values and get their keys:

for (auto& entry : obj) {
  std::cout << entry.key() << std::endl;
}

which should yield:

first
second
third
@robiwano
Copy link
Author

Sorry. I found the way to do it.

@asowani
Copy link

asowani commented Mar 21, 2019

Hi @robiwano I have come across exactly the same issue. If you don't mind, could you please share the way to do this?

@nlohmann
Copy link
Owner

Maybe the items function helps?

@robiwano
Copy link
Author

json::iterator it = obj.begin();
for(; it != obj.end(); ++it)
  cout << it.key() << endl;

@asowani
Copy link

asowani commented Mar 22, 2019

@robiwano Thanks a lot! It worked!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants