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 -
[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.
-
"QJsonDocument":http://qt-project.org/doc/qt-5/qjsondocument.html ?
-
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.