How parse json array in qt
-
Hi,
I have the following json response:
@
{
"f": "from",
"d": {
"ts": 1375432006,
"md": {
"files": [
{
"fk": "UfttRaEMFQKQERsc",
"tn": "data",
"fn": "fish",
"ct": "seafood"
}
]
},
"hm": "Sent you a packet.",
"i": "327"
},
"t": "m"
}
@I am able to parse everything else except fk, tn, fn and ct. I am using the following code for this:
@ bool isOk = false;
QVariantMap maps = Json::parse(response, isOk).toMap(); if(isOk) { QString from = maps["f"].toString(); qDebug() << from; QVariantMap data = maps["d"].toMap(); QString ts = data["ts"].toString(); qDebug() << ts; QString ct = data["hm"].toString(); qDebug() << ct; QString fk = data["i"].toString(); qDebug() << fk; QVariantMap md = data["md"].toMap(); QVariantList list = md["files"].toList(); foreach(QVariant var, list) qDebug() << var.toString();; }
@
Don't know what is wrong with this code. Can plz somebody point out where I am wrong in here?
Thanks a lot
-
You should use QJsonDocument:
http://qt-project.org/doc/qt-5.1/qtcore/qjsondocument.htmlYou should get a QJsonArray from the document and work with it easily:
http://qt-project.org/doc/qt-5.1/qtcore/qjsonarray.html -
Thanks but I am not using Qt's Json parser. I am using third party json code to parse the json data. You could see that in the code as well.
-
It's better to use the standard way. Anyway, you can use whatever method you like, if you know it well.
-
[quote author="dlfo" date="1375600100"]It's better to use the standard way. Anyway, you can use whatever method you like, if you know it well.[/quote]
I agree. It is always better to rely on the standard tools and API for such operations.
-
ok noted but can somebody plz point out what is wrong in the code?
thanks
-
Does anyone if there is anything wrong with this code? I can't parse all the elements out of it.
-
One problem is that the author needs to practice some patience. Please allow at least 48 hours for people to respond, some would argue a week to be reasonable.
-
ok.
I got it solved myself. Actually each member inside QList<QVariant> needs to be converted into a QMap first and then separate out the key-value pairs.
Thanks guys
-
Hello raj ,
i am using Json in Application, so I need JSON parser can you tell me which parser you are using?
i am using qt 4.8 on windows 7.
compiler :- MSVCthanks
-
"QJsonDocument":http://qt-project.org/doc/qt-5/qjsondocument.html ?
-
hi qxoz,
QJsonDocument is the part of Qt 5.2My setup is Qt4.8 and Win 7,MSVC compiler
So, Could you tell me for that ?
thanks :) -
I guess the thread author used this lib: http://qjson.sourceforge.net/
I think that is the best choice if you can't switch to Qt 5.