33

I am quite new to Qt and I have a very simple operation that I want to do: I have to obtain the following JSonObject:

{
    "Record1" : "830957 ",
    "Properties" :
    [{
            "Corporate ID" : "3859043 ",
            "Name" : "John Doe ",
            "Function" : "Power Speaker ",
            "Bonus Points" : ["10 ", "45", "56 ", "34", "56", "10 ", "45", "56 ", "34", "56", "10 ", "45", "56 ", "34", "56", "10 ", "45", "56 ", "34", "56", "10 ", "45", "56 ", "34", "56 ", "10", "45 ", "56", "34 ", "56", "45"]
        }
    ]
}

The JSon was checked with this Syntax and Validity checker: http://jsonformatter.curiousconcept.com/ and was found valid.

I used QJsonValue initialization of String for that and converted it to QJSonObject:

QJsonObject ObjectFromString(const QString& in)
{
    QJsonValue val(in);
    return val.toObject();
}

I am loading the JSon pasted up from a file:

QString path = "C:/Temp";
QFile *file = new QFile(path + "/" + "Input.txt");
file->open(QIODevice::ReadOnly | QFile::Text);
QTextStream in(file);
QString text = in.readAll();
file->close();

qDebug() << text;
QJsonObject obj = ObjectFromString(text);
qDebug() <<obj;

There's most certainly a good way to do this because this is not working, and I didn't find any helpful examples

3 Answers 3

60

Use QJsonDocument::fromJson:

QString data; // assume this holds the json string

QJsonDocument doc = QJsonDocument::fromJson(data.toUtf8());

If you want the QJsonObject:

QJsonObject ObjectFromString(const QString& in)
{
    QJsonObject obj;

    QJsonDocument doc = QJsonDocument::fromJson(in.toUtf8());

    // check validity of the document
    if(!doc.isNull())
    {
        if(doc.isObject())
        {
            obj = doc.object();        
        }
        else
        {
            qDebug() << "Document is not an object" << endl;
        }
    }
    else
    {
        qDebug() << "Invalid JSON...\n" << in << endl;
    }

    return obj;
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for the hint @Merlin069 ! I guess this is the way to make the conversion but so far the doc fails the check and the whole function returns an empty un-initialized object. Did you try the function ?
If it's failing to find the document as an object, then the input string is not formatted correctly according to JSON.
According to this format checher it is valid.
Use QJsonDocument::fromJson, not fromBinaryData - updated answer
@EthanKeller, check your spelling of QJsonDocument !
|
7

You have to follow this step

  1. convert Qstring to QByteArray first
  2. convert QByteArray to QJsonDocument
  3. convert QJsonDocument to QJsonObject
QString str = "{\"name\" : \"John\" }";

QByteArray br = str.toUtf8();

QJsonDocument doc = QJsonDocument::fromJson(br);

QJsonObject obj = doc.object();

QString name = obj["name"].toString();
qDebug() << name;

Comments

0

Assume your json file name is myJsonfile.json, I would go about it this way:

QFile jsonFile(":/myJsonfile.json");
jsonFile.open(QIODevice::ReadOnly | QIODevice::Text);
QByteArray data = jsonFile.readAll();
jsonFile.close();

QJsonParseError jsonError;
const auto document = QJsonDocument::fromJson(data, &jsonError);
if (jsonError.error != QJsonParseError::NoError)
{
    qDebug() << "QJsonDocument::fromJson -> " << jsonError.errorString();
}

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.