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

Cannot load json file properly #759

Closed
PowerOfCreation opened this issue Sep 29, 2017 · 3 comments
Closed

Cannot load json file properly #759

PowerOfCreation opened this issue Sep 29, 2017 · 3 comments

Comments

@PowerOfCreation
Copy link

PowerOfCreation commented Sep 29, 2017

I couldn't find any example nor documentation on how to load a json file properly. I did it doing so:
ifstream myfile(p.path());

json j;
j << myfile;
cout << j.at(0) << endl;
cout << j[0] << endl;

Neither of those variants do work. I can compile it but I get the error cannot use at() with object and same with the [] operator. Only with a iterator I can actually access my values:
for (json::iterator it = j.begin(); it != j.end(); ++it)
But I can't access the subvalues in any way yet as there are no examples or documentations avaiable.
I tried it in many ways:

json j;
j << myfile;
for (json::iterator it = j.begin(); it != j.end(); ++it) 
{
            json k;
            k << it;
            k << it.value();
...

Are there any examples how to actually load a json file properly or am I doing something wrong?

@nlohmann
Copy link
Owner

Here are examples from the README: https://github.com/nlohmann/json#tofrom-streams-eg-files-string-streams

How does your JSON file look like? What is the error message you encounter?

@nlohmann nlohmann added the state: please discuss please discuss the issue or vote for your favorite option label Sep 30, 2017
@nlohmann
Copy link
Owner

nlohmann commented Oct 4, 2017

Closing due to inactivity. Feel free to comment to this issue if the problem still occurs.

@nlohmann nlohmann closed this as completed Oct 4, 2017
@coolhandle01
Copy link

coolhandle01 commented Nov 13, 2017

@PowerOfCreation if you're still around, I'm using ifstream to stream into the JSON object, like in the examples on the readme:

json j;
std::ifstream i("path/to.json");
if (i.is_open() && i.good())
    i >> j;

It looks like you're trying to do j << i instead. Perhaps supported by the library, but I haven't tried.

@nlohmann nlohmann removed the state: please discuss please discuss the issue or vote for your favorite option label Dec 4, 2017
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