JSON Reader does not work

0

I have this code:

    QProcess process;
process.start("powershell -Command Invoke-WebRequest -Uri http://3ds.titlekeys.gq/json_enc -OutFile titles.json");
process.waitForFinished();
if (QFile::exists("titles.json"))
{
    QString content;
    QFile file("titles.json");
    file.open(QFile::ReadOnly | QFile::Text);
    content = file.readAll();
    file.close();
    QJsonDocument json = QJsonDocument::fromJson(content.toUtf8());
    QJsonObject list = json.object();
    QStringList jsontitles = list.keys();
    for (QString title : jsontitles)
    {
        QJsonObject title_data = list[title].toObject();
        QString gm9title = title_data["titleID"].toString();
        if (title_data["name"].toString() != "")
        {
            gm9title += " ";
            gm9title.append(title_data["name"].toString());
        }
        if (title_data["serial"].toString() != "")
        {
            gm9title += " ";
            gm9title += title_data["serial"].toString();
        }
        if (title_data["region"].toString() != "")
        {
            gm9title += " ";
            gm9title += title_data["region"].toString();
        }
        ui->listWidget->addItem(gm9title);
        titles.push_back(gm9title);
    }
}

But my list is empty ...

    
asked by Valentin Vanelslande 24.12.2017 в 19:20
source

1 answer

1

Assuming you enter if (QFile::exists("titles.json")) , the problem is caused because you have an array of objects:

[{
    "titleID": "0004000e000edf00",
    "serial": "CTR-U-AXCE",
    "encTitleKey": "8b54088da074d78994d558868f5742aa",
    "name": "Super Smash Bros.\u2122 Update Ver. 1.1.6",
    "region": "USA",
    "size": 344718180
}, {
    "titleID": "00040000000d7e00",
    "serial": "CTR-N-JNUP",
    "encTitleKey": "27107daf63011babf7ba603397b1889d",
    "name": "Steel Diver\u2122: Sub Wars",
    "region": "EUR",
    "size": 205773668
[...]

So you must use the array () method, then the task is similar to the initial code:

if (QFile::exists("titles.json"))
{   
    QString content;
    QFile file("titles.json");
    file.open(QFile::ReadOnly | QFile::Text);
    content = file.readAll();
    file.close();
    QJsonDocument json = QJsonDocument::fromJson(content.toUtf8());
    const QStringList keys{"name", "serial", "region"};
    QString gm9title;

    for(const QJsonValue &value : json.array()){
        if(value.isObject()){
            QJsonObject obj = value.toObject();
            gm9title =obj["titleID"].toString();
            for(const QString &key: keys){
                gm9title += obj[key].toString().isEmpty() ? "" : " "+obj[key].toString();
            }
            titles<<gm9title;
            ui->listWidget->addItem(gm9title);
        }
    }
}
    
answered by 24.12.2017 / 21:59
source