Hello,
can someone please provide me with some examples on how to traverse QVariantMap?
thanks.
Hello,
can someone please provide me with some examples on how to traverse QVariantMap?
thanks.
Qt Code:
for(QVariantMap::const_iterator iter = map.begin(); iter != map.end(); ++iter) { qDebug() << iter.key() << iter.value(); }To copy to clipboard, switch view to plain text mode
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:
Qt Code:
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(); }To copy to clipboard, switch view to plain text mode
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.
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.
Last edited by ggdev001; 7th February 2013 at 12:47.
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).
QVariant::isMap(), QVariant::toMap()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??
and then recursively descend into the new map.
ggdev001 (7th February 2013)
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:
it returns an empty string. Which seems to be due to the fact which we mentioned .....Qt Code:
if (results.contains("code")) qDebug()<<"***code***: "<<results.value("code");To copy to clipboard, switch view to plain text mode
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.
Hi, I am using https://developer.blackberry.com/cas...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.
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.
Bookmarks