Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How parse json array in qt

How parse json array in qt

Scheduled Pinned Locked Moved Mobile and Embedded
13 Posts 7 Posters 19.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    raj.qtdev
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dlfo
      wrote on last edited by
      #2

      You should use QJsonDocument:
      http://qt-project.org/doc/qt-5.1/qtcore/qjsondocument.html

      You should get a QJsonArray from the document and work with it easily:
      http://qt-project.org/doc/qt-5.1/qtcore/qjsonarray.html

      1 Reply Last reply
      0
      • R Offline
        R Offline
        raj.qtdev
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dlfo
          wrote on last edited by
          #4

          It's better to use the standard way. Anyway, you can use whatever method you like, if you know it well.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            leon.anavi
            wrote on last edited by
            #5

            [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.

            http://anavi.org/

            1 Reply Last reply
            0
            • R Offline
              R Offline
              raj.qtdev
              wrote on last edited by
              #6

              ok noted but can somebody plz point out what is wrong in the code?

              thanks

              1 Reply Last reply
              0
              • R Offline
                R Offline
                raj.qtdev
                wrote on last edited by
                #7

                Does anyone if there is anything wrong with this code? I can't parse all the elements out of it.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    raj.qtdev
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    • IamSumitI Offline
                      IamSumitI Offline
                      IamSumit
                      wrote on last edited by
                      #10

                      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 :- MSVC

                      thanks

                      Be Cute

                      1 Reply Last reply
                      0
                      • Q Offline
                        Q Offline
                        qxoz
                        wrote on last edited by
                        #11

                        "QJsonDocument":http://qt-project.org/doc/qt-5/qjsondocument.html ?

                        1 Reply Last reply
                        0
                        • IamSumitI Offline
                          IamSumitI Offline
                          IamSumit
                          wrote on last edited by
                          #12

                          hi qxoz,
                          QJsonDocument is the part of Qt 5.2

                          My setup is Qt4.8 and Win 7,MSVC compiler

                          So, Could you tell me for that ?
                          thanks :)

                          Be Cute

                          1 Reply Last reply
                          0
                          • X Offline
                            X Offline
                            Xander84
                            wrote on last edited by
                            #13

                            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.

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved