Qt parse json answer

I need to parse a json response that looks like this and get the id value, i.e. blabla2:

{ "kind": "blabla", "id": "blabla2", "longUrl": "blabla3" } 

How should I do it? I tried using Qjson, but when I try to build it to get the .dll , I get the error message:

xlocale.h is missing.

enter image description here

Are there other alternatives? Thanks.

+4
source share
2 answers

Looking at the documentation for QJsonDocument, you can read the file in QByteArray, and then do the following: -

 // assuming a QByteArray contains the json file data QJsonParseError err; QJsonDocument doc = QJsonDocument::fromJson(byteArray, &err); // test for error... 

Then use the function on the QJsonDocument to retrieve the top-level object ...

 if(doc.isObject()) { QJsonObject obj = doc.object(); QJsonObject::iterator itr = obj.find("id"); if(itr == obj.end()) { // object not found. } // do something with the found object... } 

QJsonObject also has a value () function, so instead of using an iterator and calling find, you can just call: -

 QJsonValue val = obj.value("id"); 

Disclaimer: I present this code after I just read the Qt documentation, so don't just copy and paste this, but consider it more pseudo-code. You may need to edit it a bit, but hope this helps.

+7
source

I would advise you to use Qt 5 or backport json classes for Qt 4. Your software will be more reliable in the future when you intend to port it to Qt 5, since you will need to rewrite the json parsing available in QtCore.

I would write something like the code below, but please double check it before using it in the production process, as I might have missed the error checking when writing. Regardless of error checking, the output is what you wanted to get.

main.cpp

 #include <QFile> #include <QByteArray> #include <QJsonDocument> #include <QJsonObject> #include <QDebug> int main() { QFile file("main.json"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "Could not open the file" << file.fileName() << "for reading:" << file.errorString(); return 1; } QByteArray jsonData = file.readAll(); if (file.error() != QFile::NoError) { qDebug() << QString("Failed to read from file %1, error: %2").arg(file.fileName()).arg(file.errorString()); return 2; } if (jsonData.isEmpty()) { qDebug() << "No data was currently available for reading from file" << file.fileName(); return 3; } QJsonDocument document = QJsonDocument::fromJson(jsonData); if (!document.isObject()) { qDebug() << "Document is not an object"; return 4; } QJsonObject object = document.object(); QJsonValue jsonValue = object.value("id"); if (jsonValue.isUndefined()) { qDebug() << "Key id does not exist"; return 5; } if (!jsonValue.isString()) { qDebug() << "Value not string"; return 6; } qDebug() << jsonValue.toString(); return 0; } 

main.pro

 TEMPLATE = app TARGET = main QT = core SOURCES += main.cpp 

Assembly and launch

 qmake && make && ./main 

Exit

 "blabla2" 
+2
source

Source: https://habr.com/ru/post/1493193/


All Articles