Json: Parsing string into json doesn't preserve the order correctly.

Created on 28 Oct 2019  路  7Comments  路  Source: nlohmann/json

  • Describe what you want to achieve.
    I want to parse a json file from the web, downloaded into a string using libcurl, back into a json object.

  • Describe what you tried.
    Image of code
    This is what I've tried - the former cout prints the string that curl downloaded, which is in the correct order of the original json on the webserver. The second cout simply posts the same json but as a json object, which has been parsed by nlohmann::json. As you can see, it is completely jumbled up. When trying to access a nested part of the json, for example ["teams"]["players"] it returns null and crashes with error 305, cannot use operator[] with a string argument. If you need any more information please don't hesitate to reply.

  • Describe which system (OS, compiler) you are using.
    VS2015 (v140) on Windows 10

  • Describe which version of the library you are using (release version, develop branch).
    release

question release item new feature proposed fix

Most helpful comment

It accesses the first element of an array, just like for a std::vector.

All 7 comments

This is known: https://github.com/nlohmann/json#order-of-object-keys

Ok thanks, but does this solve how to access nested Json elements? or is that something unrelated?

for example I want to access these parts, but it returns null with error 305
image

Does j["teams"][0]["players"][0]["name"] work?

Yes, that works a charm! Thanks so much! This may seem like a silly question, but what does the [0] actually do there? Thanks again.

It accesses the first element of an array, just like for a std::vector.

To be fixed with #2258.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dcube9 picture dcube9  路  3Comments

sqwunkly picture sqwunkly  路  3Comments

zkelo picture zkelo  路  3Comments

bassosimone picture bassosimone  路  3Comments

MariaRamos89 picture MariaRamos89  路  4Comments