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

Iterating over sub-object #794

Closed
fwsGonzo opened this issue Oct 21, 2017 · 2 comments
Closed

Iterating over sub-object #794

fwsGonzo opened this issue Oct 21, 2017 · 2 comments
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@fwsGonzo
Copy link

fwsGonzo commented Oct 21, 2017

I'm sorry but I'm completely lost
I just want to iterate over a value:

    auto tiles = j.find("tiles");
    if (tiles != j.end())
    {
      for (auto tile : *tiles)
      {
        std::cout << tile.key() << std::endl;
        //printf("Name: %s\n", tile.key().c_str());
      }
    }

I even used std::cout just like the examples.

{
  "tiles":
  {
    "a": [2, 5],
    "b": [3, 0],

What am I doing wrong? It says error: no member named 'key' in tile but that makes no sense because im *iterator so I should already be ranged-for-ing over each tile.
The examples also don't really provide any insight into iterating over things like these.

Iterating over the sub-object and then std::cout << tile << std::endl; works like expected, however that is just magic. I want to print the key and the value separately, one as a string and the other as an array using indexing operator.

@fwsGonzo fwsGonzo changed the title Iterating over object Iterating over sub-object Oct 21, 2017
@nlohmann
Copy link
Owner

You may want to look at iteration wrappers - here is an example how to use them https://github.com/nlohmann/json/blob/develop/doc/examples/iterator_wrapper.cpp.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Oct 22, 2017
@fwsGonzo
Copy link
Author

Thanks, worked :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants