Parse json string to QMap
-
Hi,
What exactly is your doubt ?
-
Ok, I see. Your main issue is that you are doing your work always in the same map. Once you have an object, you have to create a new map, fill it and then assign it to the current one. But ! You can't assign a QMap value to your
QMap<QString, QString>
. So your original plan can't work.That's why QJsonObject as the toVariantMap function.
You have to change your design to work with QVariantMap.
-
Ok, I see. Your main issue is that you are doing your work always in the same map. Once you have an object, you have to create a new map, fill it and then assign it to the current one. But ! You can't assign a QMap value to your
QMap<QString, QString>
. So your original plan can't work.That's why QJsonObject as the toVariantMap function.
You have to change your design to work with QVariantMap.
-
Then I may have misunderstood your current issue.
Can you show an example of the input you get and the output you expect to build from it ?
-
Then I may have misunderstood your current issue.
Can you show an example of the input you get and the output you expect to build from it ?
QMap<QString, QMap<QString, QMap<QString, QString>>> m = { {"files", { {"App.exe", {{"hash", "f976"}}}, {"D3Dcompiler_47.dll", {{"hash", "954f"}}}, {"generic", { {"qtuiotouchplugin.dll", {{"hash", "ed72"}}}, {"test", {{"hash", ""}}}, }} }} };
There's another way to convert the json back to object/array?
-
QMap<QString, QMap<QString, QMap<QString, QString>>> m = { {"files", { {"App.exe", {{"hash", "f976"}}}, {"D3Dcompiler_47.dll", {{"hash", "954f"}}}, {"generic", { {"qtuiotouchplugin.dll", {{"hash", "ed72"}}}, {"test", {{"hash", ""}}}, }} }} };
There's another way to convert the json back to object/array?
You can't dynamically create that kind of class in C++. However, if you have such a fixed known structure, then my remark about creating a new QMap each time you have to recurse.
-
You can't dynamically create that kind of class in C++. However, if you have such a fixed known structure, then my remark about creating a new QMap each time you have to recurse.
-
I just realized something, based on what you want, you might want to check hlohmann json library. It might be better suited to your needs.
-
I just realized something, based on what you want, you might want to check hlohmann json library. It might be better suited to your needs.
-
@JonB said in Parse json string to QMap:
@SGaist
Blimey! From where I am/OP is now, I would just work withQJson
andQVariantMap
!I already suggested that, but it seems not to be an option.
-
@JonB said in Parse json string to QMap:
@SGaist
Blimey! From where I am/OP is now, I would just work withQJson
andQVariantMap
!I already suggested that, but it seems not to be an option.
@SGaist
I know, I was agreeing with you :)No idea how much JSON data OP has to parse. They might read it in as
QVariantMap
and transform in code to some specificQMap<..., ...>
if demanded. Not the fastest, but might be quickest if OP wants to write it. Integrating the code you referenced has its own overheads :)