Unsolved Create Json with particular structure with C ++
-
Hello everyone, could someone tell me if there is an external qt and c ++ library to create a json with a particular structure, I work with QJsonDocument but the variables are ordered alphabetically.
-
Hi,
You might want to check the excellent nlohmann json project.
-
@Gabriela20 said in Create Json with particular structure with C ++:
I work with QJsonDocument but the variables are ordered alphabetically.
May I ask why you need a particular order?
In JSON, Objects are unordered structures. Therefore
{"key1": 1, "key2": 2}
is the same value as{"key2": 2, "key1": 1}
-
@JKSH I am working with Azure And I only accept a specific structure so that Azure can work
-
@Gabriela20
Seems very strange, since JSON is unordered. I assume you'll have to implement your own writer. -
@SGaist I am a bit new to qt, could you explain me how to install the library
-
@JonB it works with QJsonDocument and sorts them alphabetically
-
@Gabriela20
What works with QJsonDocument and what sorts them alphabetically?
Even if something outputs JSON sorted alphabetically --- and anything is free to do so, since JSON is unordered --- does that mean any reader requires the input to be sorted alphabetically in order to work? It certainly should not require this if it supports JSON. -
@JonB Internally QJsonObject converted json objects to a QVariantMap (QMap(QString,QVariant)). This is always sorted by key: "With QMap, the items are always sorted by key."
https://doc.qt.io/qt-5/qjsonobject.htmlSo if reading a json document with QJsonDocument you will always get sorted maps of objects. I don't see why this is an issue though. You can access the values in any order.
-
@Gabriela20 said in Create Json with particular structure with C ++:
I am working with Azure And I only accept a specific structure so that Azure can work
Really? I would've expected Azure to accept unordered JSON... Do you have a link to any documentation (or an example) that shows that Azure expects a specific order?
-
@fcarney said in Create Json with particular structure with C ++:
So if reading a json document with QJsonDocument you will always get sorted maps of objects.
Indeed. But that is only an implementation. The fact remains that JSON specifies no order for keys.
I am with @JKSH
Really? I would've expected Azure to accept unordered JSON... Do you have a link to any documentation (or an example) that shows that Azure expects a specific order?
I would like to see @Gabriela20 explain/substantiate
I am working with Azure And I only accept a specific structure so that Azure can work
If whatever s/he is using in Azure to read is JSON-compliant, where does it require, rather than accept, alphabetically-ordered keys?
-
I use the nlohmann template class library...in fact, it is going to the moon.
-
@Gabriela20 It is a header-only library. Include the header file(s) (per instructions from the git project page) and have fun.