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
this the the response i need to get mobile serial value from this response how will it be achieved my code isQJsonDocument 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
and i tested it in qt works fine
but when i try to catch serial no way i get undefined
where is the error now why it didn't get it directly.thanks again -
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
-
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)
- TbMobileSerial (Array)
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 a
QJsonValue
and 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 155I hope i've helped you to solve your doubt. Good studies and good coding.
- TbMobile (Object)
-
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
and i tested it in qt works fine
but when i try to catch serial no way i get undefined
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 = ObjectSo, you should access the data using:
document.array()[0].toObject().value("mobileSerial").toString();