Qt Parsing Json Object
-
I am trying to parse json object in QT from an api. However when i try the codes written below i cannot parse the object. I simply want to get the information stored in those parameters. Please help me thank you !!!
API is
{ "error": { "errorcode": 0, "errorstring": "" }, "login": 1, "logintoken": "209b06aa7f059673db393ff7a731af1847344903b9733f026cc3a6f7a5b797b3" }
The Codes are
QNetworkReply* reply = manager->post(ava_request,postData); loop.exec(); QString response = (QString)reply->readAll(); qDebug() << response; QJsonDocument temp = QJsonDocument::fromJson(response.toUtf8()); QJsonObject jsonObj = temp.object(); qDebug() <<"error"<< jsonObj["error"].toString(); qDebug() <<"login"<< jsonObj["login"].toString(); qDebug() << "logintoken"<<jsonObj["logintoken"].toString();
the response string looks
and the output is
D/libAndroisShop.so(21944): ../AndroisShop/networkconnection.cpp:45 (QString NetworkConnection::Connect(QString, QString)): "<br><br>NO OF ROWDATA: 1{\"error\":{\"errorcode\":0,\"errorstring\":\"\"},\"login\":1,\"logintoken\":\"4daaf6b3dd5a26a2ad2c436e564bfa4d6c439ce4d0d6cd66705a8bdadddddaa0\"}" D/libAndroisShop.so(21944): ../AndroisShop/networkconnection.cpp:50 (QString NetworkConnection::Connect(QString, QString)): error "" D/libAndroisShop.so(21944): ../AndroisShop/networkconnection.cpp:51 (QString NetworkConnection::Connect(QString, QString)): login "" D/libAndroisShop.so(21944): ../AndroisShop/networkconnection.cpp:52 (QString NetworkConnection::Connect(QString, QString)): logintoken ""
-
Your JSON is mix of array & objects. You need split them properly. I have given the code here. It is not optimised.
int main(int argc, char *argv[]) { QApplication a(argc, argv); MyWidget w; w.show(); QString json = "{\"error\":{\"errorcode\":0,\"errorstring\":\"\"},\"login\":1,\"logintoken\":\"4daaf6b3dd5a26a2ad2c436e564bfa4d6c439ce4d0d6cd66705a8bdadddddaa0\"}"; QJsonDocument temp = QJsonDocument::fromJson(json.toUtf8()); if (temp.isArray()){ qDebug() << " It is an array" <<endl; } if (temp.isObject()){ qDebug() << " It is an Object" <<endl; } QJsonArray arr = temp.array(); qDebug() << " Array size = " << arr.size() <<endl; QJsonObject obj = temp.object(); qDebug() << " Object size = " << obj.size() <<endl; qDebug() << " Object size = " << obj.keys() <<endl; QJsonObject::iterator it; for(it=obj.begin();it!=obj.end();it++){ if(it.value().isObject()){ QJsonObject o1 = it.value().toObject(); qDebug() << " it is JSON Object" <<endl; qDebug() << " Key = " << it.key() << " Val =" << it.value() <<endl; QJsonObject::iterator it1 = o1.begin(); for(it1=o1.begin();it1!=o1.end();it1++){ qDebug() << " Key =" << it1.key(); if (it1.value().isString()) { qDebug() << " Value =" << it1.value().toString() <<endl; } if (it1.value().isDouble()) { qDebug() << " Value =" << it1.value().toInt() <<endl; } } }else { qDebug() << " Key =" << it.key() << " Value =" << it.value() <<endl; if (it.value().isString()){ qDebug() << " Value = " << it.value().toString() <<endl; } if (it.value().isDouble()){ qDebug() << " Value = " << it.value().toInt() <<endl; } } } return a.exec();
-
@Naresh said in Qt Parsing Json Object:
and the output is
...
"
<br><br>NO OF ROWDATA: 1{\"error\":{\"errorcode\":0,\"errorstring\":\"\"},\"login\":1,\"logintoken\":\"4daaf6b3dd5a26a2ad2c436e564bfa4d6c439ce4d0d6cd66705a8bdadddddaa0\"}
"This is not a valid JSON document, so you cannot pass it into QJsonDocument::fromJson().
You must first remove the non-JSON string in front:
"<br><br>NO OF ROWDATA: 1
" -
@dheerendra said in Qt Parsing Json Object:
Your JSON is mix of array & objects.
There are no arrays in the @Naresh's example
-
@JKSH You are right. No arrays.
-
QJsonDocument doc = QJsonDocument::fromJson(response); QJsonObject JON = doc.object(); QString error_code = JON.value("error").toObject().value("errorcode").toString(); QString error_string = JON.value("error").toObject().value("errorcode").toString(); int login = JON.value("login").toInt(); QString login_token = JON.value("logintoken").toString();
-
@dheerendra Hi, I am new to Qt and Learning Json Parsing.
To Parse this https://reqres.in/api/users/2 ,i've used your code sample. But it didn't worked. Help me solve this -
@Stephen28
Hello and welcome.This is an old thread. I suggest you open your own new topic. Paste in the
{"data":{"id":2,"email":"janet.weaver@reqres.in","first_name":"Janet","last_name":"Weaver","avatar":"https://reqres.in/img/faces/2-image.jpg"},"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}
you are trying to parse. Show how far you got, e.g. does
QString input = R"( {"data":{"id":2,"email":"janet.weaver@reqres.in","first_name":"Janet","last_name":"Weaver","avatar":"https://reqres.in/img/faces/2-image.jpg"},"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}} )"; QJsonDocument doc = QJsonDocument::fromJson(input); QJsonObject JON = doc.object();
at least work to get the top-level object?