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

read in .json file yields strange sizes #487

Closed
CDitzel opened this issue Mar 7, 2017 · 23 comments
Closed

read in .json file yields strange sizes #487

CDitzel opened this issue Mar 7, 2017 · 23 comments

Comments

@CDitzel
Copy link

CDitzel commented Mar 7, 2017

I am trying to read in an .json file into an json object via the overloaded >> operator

I can also write it to another .json file with << which reproduces the same file, as expected.

however, when I try to find out about the size of the json-container, it yields 3, although my file is quite big

json input; i >> input; std::cout << input.size() << input << std::endl;

it gives the correct content but the size is obviously wrong

@CDitzel
Copy link
Author

CDitzel commented Mar 7, 2017

Ok I narrowed down the problem. My input looks as follows, and apparently only the imgHeight, imgWidt and objectsare counted as entries. How can I then access the inner elements in the json container?

{ "imgHeight": 1024, "imgWidth": 2048, "objects": [ { "label": "road", "polygon": [ [ 0, 769 ], [ 290, 574 ], [ 93, 528 ], [ 0, 524 ], [ 0, 448 ], [ 0, 448 ], [ 210, 453 ], [ 511, 451 ], [ 782, 459 ], [ 892, 495 ], [ 943, 498 ], [ 1044, 471 ], [ 1490, 486 ], [ 1880, 519 ], [ 1922, 512 ], [ 1975, 522 ], [ 2047, 524 ], [ 2047, 570 ], [ 1668, 553 ], [ 2047, 597 ], [ 2047, 1023 ], [ 892, 1023 ], [ 0, 1023 ] ] } ] }

@nlohmann
Copy link
Owner

nlohmann commented Mar 7, 2017

You can try input["objects"].size().

@CDitzel
Copy link
Author

CDitzel commented Mar 7, 2017

does not work. I get 0% returned

@nlohmann
Copy link
Owner

nlohmann commented Mar 7, 2017

What gives input.dump()?

@CDitzel
Copy link
Author

CDitzel commented Mar 7, 2017

thank you for your quick reply!

it yields the content of the entire file

@nlohmann
Copy link
Owner

nlohmann commented Mar 7, 2017

Of what do you want to get the size then?

@CDitzel
Copy link
Author

CDitzel commented Mar 7, 2017

cuz I want to access the range of numeric tuples in the inner most element and write them into a new json file

@nlohmann
Copy link
Owner

nlohmann commented Mar 7, 2017

I do not understand your problem. If the input file is the one of your second comment and you want to have the number of elements in the objects array, then input["objects"].size() would give you this.

@CDitzel
Copy link
Author

CDitzel commented Mar 7, 2017

well for me it does not, it gives 1%.

I somehow need a way to access the tuples in the inner array

@nlohmann
Copy link
Owner

nlohmann commented Mar 7, 2017

How can this function return a percentage? What array do you mean? I tried to be concrete, but you only told me that it does not work.

@CDitzel
Copy link
Author

CDitzel commented Mar 7, 2017

I will try to elaborate, wait a second

@CDitzel
Copy link
Author

CDitzel commented Mar 7, 2017

well ok in the shell it showed 1%, in a file it is displayed as 1, which is correct I guess.

But the main problem remains: How can I access elements of a json file that are cascaded deep in several layers of arrays, e.g. how can I access in my given file just the number pairs by means of the json-container?

@nlohmann
Copy link
Owner

nlohmann commented Mar 7, 2017

input["objects"][0]["polygon"].size()

@gregmarr
Copy link
Contributor

gregmarr commented Mar 7, 2017

well ok in the shell it showed 1%, in a file it is displayed as 1, which is correct I guess.

My Psychic Debugging skills (see The Old New Thing) says that you did

printf("%d", input["objects"].size());

at the end of your program, and that because you didn't include "\n", you got the 1 immediately followed by your shell prompt %.

@CDitzel
Copy link
Author

CDitzel commented Mar 7, 2017

Danke Niels und viele Grüße nach Berlin!

@gregmarr: No my shell is not the culprit

@nlohmann
Copy link
Owner

nlohmann commented Mar 7, 2017

Grüße zurück! Does it work now?

@CDitzel
Copy link
Author

CDitzel commented Mar 7, 2017

I think so, I will have to further investigate. I am given a messed up json file that I have to clean up and bring into a shape comparable to the one I posted above, so I will see if I can manage

@CDitzel
Copy link
Author

CDitzel commented Mar 7, 2017

one last question: I am trying to achieve a similar output format as in my example above, where there are two braces following each other, namely " [ {". But I cannot figure out how to put this in the code in order to get the desired output

@nlohmann
Copy link
Owner

nlohmann commented Mar 7, 2017

You can use [] for both arrays and objects. For arrays, you pass the index (starting with 0) as parameter, for objects the key (i.e., a string). In the example above:

input["objects"][0]["polygon"]
  ^       ^      ^      ^
  |     a key    |      |
  |          an index   |
an object             a key
  • input is an object
  • input["objects"] is an array
  • input["objects"][0] is an object
  • input["objects"][0]["polygon"] is an array

@CDitzel
Copy link
Author

CDitzel commented Mar 7, 2017

awesome. Thank you so much niels

@nlohmann
Copy link
Owner

nlohmann commented Mar 7, 2017

Can I close the issue?

@CDitzel
Copy link
Author

CDitzel commented Mar 7, 2017

if I am allowed to open up another one in case of further questions, yes ; )

@nlohmann
Copy link
Owner

nlohmann commented Mar 7, 2017

Of course :)

@nlohmann nlohmann closed this as completed Mar 7, 2017
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