Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

get json value from api response



  • 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



  • 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


  • Qt Champions 2019

    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



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



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



  • 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



  • @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();
    


  • Thank you so much for your helping


Log in to reply