Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. get json value from api response

get json value from api response

Scheduled Pinned Locked Moved Solved General and Desktop
qtcreator
7 Posts 3 Posters 5.1k Views
  • 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.
  • M Offline
    M Offline
    Mr.alaa
    wrote on 23 Sept 2018, 15:28 last edited by
    #1

    hello everyone i have API where i can get data from json response and it works fine i need to extract only one value i have tried many ways but i can't get it
    0_1537799148141_46f61443-c9aa-493f-84e2-d97ea151aa9f-image.png
    this the the response i need to get mobile serial value from this response how will it be achieved my code is

    QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
    
    
                   QJsonValue v4 =  document.object().value("TbMobile");
                   qDebug() << "v4 = " << v4;
    

    i hope to find answer for that

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mr.alaa
      wrote on 23 Sept 2018, 19:16 last edited by
      #5

      thank you for you reply so much and thanks for welcome me here but through this period i have changed my api and now returned what i only need
      0_1537812820256_2cf7d8c5-c887-49f4-b462-ee78d5b89bda-image.png
      and i tested it in qt works fine
      0_1537812877393_01c7e5fe-f0c0-44a0-8d3a-e8c9bc48270f-image.png
      but when i try to catch serial no way i get undefined
      0_1537812960252_c4476a6e-defe-4c28-be70-647e9fa1a871-image.png
      where is the error now why it didn't get it directly.thanks again

      1 Reply Last reply
      0
      • C Online
        C Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 23 Sept 2018, 15:32 last edited by
        #2

        QJsonDocument::fromJson() has a second, optional QJsonParseError parameter which can be used to find out if the document could be parsed correctly. You should check it. http://doc.qt.io/qt-5/qjsondocument.html#fromJson

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        3
        • M Offline
          M Offline
          Mr.alaa
          wrote on 23 Sept 2018, 15:46 last edited by
          #3

          can you help me with example that it is the first time to use api

          1 Reply Last reply
          0
          • K Offline
            K Offline
            KillerSmath
            wrote on 23 Sept 2018, 18:13 last edited by
            #4

            Hi @Mr-alaa and welcome to Qt Forum.

            As mentioned by @Christian-Ehrlicher, you can use a ParseError variable to check if you have an error in parse

            // in header section
            #include <Qfile>
            #include <QJsonParseError>
            #include <QJsonDocument>
            #include <QDebug>
            
            // in body of function
                 QByteArray loadData = loadFile.readAll(); // from QFile
                 QJsonParseError jsonError;
                 QJsonDocument loadDoc = QJsonDocument::fromJson(loadData, &jsonError); // parse and capture the error flag
            
                  if(jsonError.error != QJsonParseError::NoError){ 
                    qDebug() << "Error: " << jsonError.errorString();
                    //return; 
                 }
            

            Okay, you've used the errorParse variable to check if your parse has an error but your json data is sure.
            So, we can concentrate in how you should to extract the information from this json.

            Note that you are trying to access the data within another dataset, as shown below:

            • TbMobile (Object)
              • TbMobileSerial (Array)
                • MobileSerial (String)

            You may use 2 ways to access a member of a Json Object

            • value(key) function -> object.value(key)
            • [] operator -> object[key]

            But firstly, the return of these operations is aQJsonValueand this object needs to be converted back to a QJsonObject or QJsonArray, for example.

            Suppose you have a json file like that:

            {
               "obj1": {
                  "name": "A String",
                  "value": 2,
                  "array" : [
                    {
                       "id": 152,
                       "description": "A small description of element 152"
                    },
                    {
                       "id": 155,
                       "description": "A small description of element 155"
                    }
                  ]
              }
            }
            

            And you need to access the description of second element of array object that is a member of obj1.

            obj1 -> array[1] -> description
            

            You could to access this information using a similar code:

            /// in header section of your file
            #include <QJsonDocument>
            #include <QJsonValue>
            #include <QJsonObject>
            #include <QJsonArray>
            #include <qDebug>
            
            // in body of your function
            
                 QJsonObject rootObject = loadDoc.object();
                 QJsonObject obj1Data = rootObject["obj1"].toObject();
                 QJsonArray arrayData = obj1Data["array"].toArray();
                 QString description = arrayData[1].toObject()["description"].toString();
                 qDebug() << description;
                 
            

            The output:
            A small description of element 155

            I hope i've helped you to solve your doubt. Good studies and good coding.

            @Computer Science Student - Brazil
            Web Developer and Researcher
            “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

            1 Reply Last reply
            4
            • M Offline
              M Offline
              Mr.alaa
              wrote on 23 Sept 2018, 19:16 last edited by
              #5

              thank you for you reply so much and thanks for welcome me here but through this period i have changed my api and now returned what i only need
              0_1537812820256_2cf7d8c5-c887-49f4-b462-ee78d5b89bda-image.png
              and i tested it in qt works fine
              0_1537812877393_01c7e5fe-f0c0-44a0-8d3a-e8c9bc48270f-image.png
              but when i try to catch serial no way i get undefined
              0_1537812960252_c4476a6e-defe-4c28-be70-647e9fa1a871-image.png
              where is the error now why it didn't get it directly.thanks again

              1 Reply Last reply
              0
              • K Offline
                K Offline
                KillerSmath
                wrote on 23 Sept 2018, 19:33 last edited by
                #6

                @Mr-alaa
                It is because your api is returning an array as the root element

                [] Delimiter = Array
                {} Delimiter = Object

                So, you should access the data using:

                document.array()[0].toObject().value("mobileSerial").toString();
                

                @Computer Science Student - Brazil
                Web Developer and Researcher
                “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                1 Reply Last reply
                2
                • M Offline
                  M Offline
                  Mr.alaa
                  wrote on 23 Sept 2018, 20:39 last edited by
                  #7

                  Thank you so much for your helping

                  1 Reply Last reply
                  0

                  1/7

                  23 Sept 2018, 15:28

                  • Login

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