Results 1 to 10 of 10

Thread: Traversing QVariantMap

  1. #1
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Traversing QVariantMap

    Hello,

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

    thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Traversing QVariantMap

    Qt Code:
    1. for(QVariantMap::const_iterator iter = map.begin(); iter != map.end(); ++iter) {
    2. qDebug() << iter.key() << iter.value();
    3. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: Traversing QVariantMap

    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:
    1. const QByteArray response(reply->readAll());
    2. qDebug() <<"Response: "<< response;
    3.  
    4.  
    5. JsonDataAccess jda;
    6. QVariantMap results = jda.loadFromBuffer(response).toMap();
    7. for(QVariantMap::const_iterator iter = results.begin(); iter != results.end(); ++iter) {
    8. qDebug() << iter.key() << iter.value();
    9. }
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Traversing QVariantMap

    The first line of your code looks ok. What does response.size() return?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: Traversing QVariantMap

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Traversing QVariantMap

    Quote Originally Posted by ggdev001 View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    ggdev001 (7th February 2013)

  8. #7
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: Traversing QVariantMap

    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:
    Qt Code:
    1. if (results.contains("code"))
    2. qDebug()<<"***code***: "<<results.value("code");
    To copy to clipboard, switch view to plain text mode 
    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.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Traversing QVariantMap

    Quote Originally Posted by ggdev001 View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: Traversing QVariantMap

    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.

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Traversing QVariantMap

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 0
    Last Post: 10th December 2011, 11:17
  2. Replies: 0
    Last Post: 16th May 2010, 17:53
  3. Traversing QTableView
    By junxuan in forum Qt Programming
    Replies: 2
    Last Post: 1st August 2009, 18:44
  4. QVariant, QVariantMap, and QFileInfoList
    By themolecule in forum Qt Programming
    Replies: 2
    Last Post: 13th September 2007, 07:00
  5. Traversing a QTreeWidget
    By johnny_sparx in forum Newbie
    Replies: 5
    Last Post: 26th March 2006, 09:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.