PDA

View Full Version : Traversing QVariantMap



ggdev001
7th February 2013, 11:15
Hello,

can someone please provide me with some examples on how to traverse QVariantMap?

thanks.

wysota
7th February 2013, 11:30
for(QVariantMap::const_iterator iter = map.begin(); iter != map.end(); ++iter) {
qDebug() << iter.key() << iter.value();
}

ggdev001
7th February 2013, 11:57
Hi thanks for your response.
I have one small issue before I could test your code and would appreciate greatly if you could help.

I have following code to (1) read data from a "QNetworkReply* reply" and also (2) parse it to JSON:


const QByteArray response(reply->readAll());
qDebug() <<"Response: "<< response;


JsonDataAccess jda;
QVariantMap results = jda.loadFromBuffer(response).toMap();
for(QVariantMap::const_iterator iter = results.begin(); iter != results.end(); ++iter) {
qDebug() << iter.key() << iter.value();
}


The problem is that first qDebug() on line 2 outputs an empty string and also the loop never enters on line 7 probably due to this. The
strange/interesting thing is that if I write to a file: reply->readAll(), then everything seems to work fine, at least the file gets written with correct JSON data (but I still have to parse it).
Could you please tell me what could be the problems with the code above? based on what I said? thanks.

wysota
7th February 2013, 12:03
The first line of your code looks ok. What does response.size() return?

ggdev001
7th February 2013, 12:42
I didn't check that one, but I removed this code which I had before the code I posted above:
// printf(reply->readAll());
// mFile->write(reply->readAll());
// mFile->flush();
// mFile->close();
and now the QByteArray I mentioned above seems to be correctly initialized -- strange huh ???

And please also one thing. As I mentioned the reply contains json data. It seems, there is one key
first, say "some key" and it's value could be a QVariant which may contain other nested QVariantMaps etc.

My question is imagine I want to get the value of key which is located inside the nested QVariantMaps. How do I do this??

For example, I have qDebug()<<"forename: "<<results.value("forename").toString(); but it returns an empty string.
But as I said I think this maybe because "forename" is a key in one of the nested QVariantMaps inside the "results"
How to behave in this case? Thank you very much.

wysota
7th February 2013, 13:10
I didn't check that one, but I removed this code which I had before the code I posted above:
// printf(reply->readAll());
// mFile->write(reply->readAll());
// mFile->flush();
// mFile->close();
and now the QByteArray I mentioned above seems to be correctly initialized -- strange huh ???
No, not at all. First you read all the content and print it out, then you try to read again (which obviously returns nothing because you have already read everything) and try to print it to a file. Then you probably tried to read again (which again returned an empty byte array).


And please also one thing. As I mentioned the reply contains json data. It seems, there is one key
first, say "some key" and it's value could be a QVariant which may contain other nested QVariantMaps etc.

My question is imagine I want to get the value of key which is located inside the nested QVariantMaps. How do I do this??
QVariant::isMap(), QVariant::toMap()

and then recursively descend into the new map.

ggdev001
7th February 2013, 13:27
Thanks, yes it seems I was trying to read the reply twice and that's why I was getting that effect.

I will try your approaches with isMap and toMap. Just to again highlight what kind of problem I had,
for example the entry in the JSON file is smth like this:
"Key: "result" Value: QVariant(QVariantMap, QMap(("code", QVariant(qlonglong,
1) ) ) )"
after printing them as you suggested.

Now, what I am actually interested in, is the value of "code" which is 1. So, when I try to run
the following command:


if (results.contains("code"))
qDebug()<<"***code***: "<<results.value("code");
it returns an empty string. Which seems to be due to the fact which we mentioned .....

So, you recommend to use isMatp and etc. right??? I will see that approach now, because my JSON
file has a loft of such nested QVariantMaps -- thanks again.

ps. Do you have maybe some samples on the web site or know links which show how to
(recursively, as it appears now) parse the whole JSON file ??? Thanks.

wysota
7th February 2013, 14:49
ps. Do you have maybe some samples on the web site or know links which show how to
(recursively, as it appears now) parse the whole JSON file ??? Thanks.

Qt5 can directly read JSON data and parse it to QVariant or QJsonDocument, for Qt4 there is either the QJson project or you can use QtScript to parse a json string to a javascript object.

ggdev001
7th February 2013, 14:53
Hi, I am using https://developer.blackberry.com/cascades/documentation/device_platform/data_access/working_with_json.html, because
I am doing BlackBerry 10 development.
Yes, indeed I think JasonDataAcess class in my case returns QVariant as the root element of the JSON data; this QVariant on the other
hand contains a QVariantMap in my case.
My problem starts that this QVariantMap has many nested elements inside (including other QVariantMaps), and I want to be able
to retrieve values associated with the keys, of these QVariantMaps. that's why it would be helpful for me to have some tutorial or code sample on it... thanks once again.

wysota
7th February 2013, 17:07
I already gave you everything you need. Provided that you know what "recursively" means, you should have no problems in accessing any data in the map that you need.