-1

I am tring Qt5. I try to read values from a json file. Json file is like the one above:

test.json

[{"w":188,"h":334,"th":0.350000,"l":232,"r":420,"t":133,"b":467,"p":0.713963,"n":"person"}]
[{"w":127,"h":141,"th":0.350000,"l":1152,"r":1279,"t":162,"b":303,"p":0.408129,"n":"person"},{"w":179,"h":339,"th":0.350000,"l":230,"r":409,"t":131,"b":470,"p":0.698172,"n":"person"}]

It's under code I try. How to read such a json file structure ?

QString val;
QFile file;
file.setFileName("test.json");
file.open(QIODevice::ReadOnly | QIODevice::Text);
//file is readall
val = file.readAll();
file.close();
qWarning() << val; //print consol
QJsonDocument jsonDocument = QJsonDocument::fromJson(val.toUtf8());
//get data array !!!
QJsonObject jsonObject = jsonDocument.object();
QJsonArray jsonArray = jsonObject["w"].toArray();
qWarning() << jsonArray[0].toString();
8
  • What is the actual question? What doesn't work as you would expect and why? Commented Jun 26, 2017 at 20:40
  • 2
    Possible duplicate of Qt parsing JSON using QJsonDocument, QJsonObject, QJsonArray Commented Jun 26, 2017 at 20:54
  • This text does not respect the json format. [values1] [values2] not json Commented Jun 26, 2017 at 20:56
  • You can verify if it is an appropriate json format in the following link: jsonlint.com Commented Jun 26, 2017 at 20:57
  • Okey. My my json file format, every line [blank] or [object {1}] or [{1} object, the object {2} ....] can form. I am trying to read this format. But I could not read it properly in this format Qt5. How to read in this format. Commented Jun 26, 2017 at 21:42

1 Answer 1

1

Since the data does not have a JSON format (it is ill-formed, see RFC 7159), but if it is in parts, what we must do is to separate it, for that we use QRegularExpresion, and we verify that the data has an appropriate format, then the code is similar to your code.

Code:

#include <QCoreApplication>
#include <QFile>
#include <QDebug>
#include <QRegularExpression>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>

int main(int argc, char *argv[]){

    QCoreApplication a(argc, argv);

    QFile file("test.json");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
        qWarning() << "Could not open file!";
        return 0;
    }
    const auto& data = QString(file.readAll());
    file.close();

    QRegularExpression regex("\\[|\\]");
    const auto& jsons = data.split(regex);

    for(const auto& json : jsons)
        if(!json.trimmed().isEmpty()){
            const auto& formattedJson = QString("[%1]").arg(json);
            const auto& doc = QJsonDocument::fromJson(formattedJson.toUtf8());

            if(doc.isArray())
                for(const auto& item : doc.array()){
                    const auto& obj = item.toObject();
                    const auto& keys = obj.keys();

                    for(const auto& key : keys){
                        if(key == "n")
                            qDebug() << key << obj[key].toString();
                        else
                            qDebug() << key << obj[key].toInt();
                    }
                }
        }

    return a.exec();
}

Output:

"b" 467
"h" 334
"l" 232
"n" "person"
"p" 0
"r" 420
"t" 133
"th" 0
"w" 188
"b" 303
"h" 141
"l" 1152
"n" "person"
"p" 0
"r" 1279
"t" 162
"th" 0
"w" 127
"b" 470
"h" 339
"l" 230
"n" "person"
"p" 0
"r" 409
"t" 131
"th" 0
"w" 179
Sign up to request clarification or add additional context in comments.

5 Comments

Why not split at '\n' instead of using regular expression?
@Azeem I'm assuming the most general case, I also thought of applying the same thing but the data may be in a single line, my solution covers both cases.
@Azeem It's always good to think about the worst case, the only one I can be sure of is that the date will be between [].
@eyllanesec: I got your point and for the given ill-formed JSON format it seems okay.
Thank you for anwer. I guess it's gonna work. @eyllanesec

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.