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

Confused about iterating through json objects #49

Closed
danpolanco opened this issue Mar 17, 2015 · 9 comments
Closed

Confused about iterating through json objects #49

danpolanco opened this issue Mar 17, 2015 · 9 comments

Comments

@danpolanco
Copy link

If I have:

{
  "england": {
    "name": "euro",
    "value": 57.99
  },
  "mexico": {
    "name": "peso",
    "value": 31.99
  },
  "us": {
    "name": "USD",
    "value": 42.99
  }
}

And I want to iterate through each, how would I do so?

My attempt so far has been similar to the following:

std::ifstream input_file( const string file_path );
nlohmann::json currencies_json;
currencies_json << input_file;
input_file.close();

unordered_map <string, currency_class> currencies;
for ( const auto currency_json: currencies_json ) {
    currency_class new_currency;

    new_currency[ "country" ] = ???
    new_currency[ "name" ] = ???
    new_currency[ "value"] = ???

    currencies[ country ] = new_currency;
}

I've tried it a a few ways including - for example - creating a string for new_currency[ "country" ] first and then storing it in it like new_currency[ "country" ] = my_string, but I get errors such as:

no member named 'key' in 'nlohmann::basic_json<std::map, std::vector, std::__1::basic_string<char>, bool, long long, double, std::allocator>'

or

[....]:1073:20: error: 'm_it' is a private member of 'nlohmann::basic_json<std::map, std::vector, std::__1::basic_string<char>,
      bool, long long, double, std::allocator>::const_iterator'
           result.m_it.object_iterator = m_value.object->find(key);
                   ^
[...]:89:32: note: in instantiation of member function 'nlohmann::basic_json<std::map, std::vector, std::__1::basic_string<char>, bool, long long, double,
      std::allocator>::find' requested here
                const auto iterator = currencies.find("country");
                                             ^
[...]/build/dependencies/json-prefix/src/json/src/json.hpp:3007:96: note: declared private here
        internal_iterator<typename array_t::const_iterator, typename object_t::const_iterator> m_it;

Could you provide some guidance? I'd really appreciate it!

@danpolanco
Copy link
Author

Here is an extended example.

@nlohmann
Copy link
Owner

Dear @DanTheColoradan

Thanks for reporting. I'll check your issue once I'm back from holidays (next week).

All the best
Niels

@gnzlbg
Copy link

gnzlbg commented Mar 20, 2015

Duplicate of #43 . The workaround is basically to (there is a better workaround for clang in the original issue):

#define private public
#include <json.hpp>
#define private private

@nlohmann
Copy link
Owner

Thanks! I'll fix it as soon as possible.

@gnzlbg
Copy link

gnzlbg commented Mar 20, 2015

@nlohmann No rush, we have a workaround. Enjoy the holidays :P

@danpolanco
Copy link
Author

Thanks @gnzlbg! And I agree @nlohmann, the workaround is pretty simple so enjoy your vacation :) A new season is upon us.

@gnzlbg
Copy link

gnzlbg commented Mar 24, 2015

This issue should have been closed by bb2671d.

@nlohmann
Copy link
Owner

All iterators have now member functions key() to retrieve the key for objects and value() to retrieve the value. This should fix the mentioned issue.

@danpolanco
Copy link
Author

Awesome. Thank you!

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

No branches or pull requests

3 participants