6

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

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

How do I do that? I tried to use Qjson, but when I try to buid it to get the .dll, I get an error:

xlocale.h is missing.

enter image description here

Are there other alternatives? Thanks.

5
  • Have you tried using the QJsonDocument class? Commented Jul 24, 2013 at 13:02
  • no i haven't, but are there any examples? Commented Jul 24, 2013 at 13:08
  • 2
    Qt4 or Qt5? QJsonDocument comes with Qt5, but isn't available in Qt4 (where qjson is most commonly used I think) Commented Jul 24, 2013 at 13:35
  • @FrankOsterfeld, I assumed Qt5, due to the link to ddacot's makefile, but good question. Commented Jul 24, 2013 at 13:55
  • ddacot, are you using Qt 4 or 5? Commented Dec 22, 2014 at 7:41

2 Answers 2

9

Looking at the documentation for QJsonDocument you can read the file into a 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...

}

There is also a value() function in QJsonObject, so instead of using the iterator and calling find, you may simply be able to call: -

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

Disclaimer: I present this code after having just read the Qt documentation, so do not just copy and paste this, but consider it more as pseudo code. You may need to edit it a little, but hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

3

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

I would write something like the code below, but please double check it before using it in production as I may have missed an error checking while writing it. 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

Build and Run

qmake && make && ./main

Output

"blabla2"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.