Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[solved] QJsonDocument not loading fromJson



  • Hi,

    I have read the other posts on here regarding problems with Qt5 and QJsonDocument but I am having a problems with it and none of the posts seem to help. Its not loading any content form the fromJson static method. No errors are reported QParseError.

    Here is the code I have:
    @
    QFile file;
    file.setFileName("c:\jsondata\data.txt");
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QString val = QString::fromUtf8(file.readAll());

    QJsonParseError parseErr;

    QJsonDocument jsonDoc = QJsonDocument::fromJson(val.toUtf8(),&parseErr);
    file.close();

    QJsonObject jsonObject = jsonDoc.object();
    QJsonArray jsonArray = jsonObject["server"].toArray();
    @

    But jsonDoc is always null.

    Any ideas why that might be, Hope someone can help me and I havent done something really stupid.

    Many Thanks

    Dave


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What does parseErr return ?

    Also why store in a QString to revert back to a QByteArray ?



  • Many thanks for getting back so fast.

    &ParseErr has a value of "no error".

    I have purposely added in characters into the text to produce and error and it does catch it when the json in the file is invalid, so I know that its getting that far and able to try and parse the json.

    As for converting from QByteArry to QString and back that was just to check the text from the file was successfully being parsed. I have tried using the file.readAll() in fromJson and it still does not work..

    Thanks

    Dave


  • Lifetime Qt Champion

    What does data.txt contain ?



  • I copied if from one of the previous QJsonDocument questions posted here as it worked form that poster:

    {
    "osc_port": 57120,
    "server": {
    "path": "/path/to/executable",
    "ip": "127.0.0.1",
    "port": 54321
    },
    "data_path": "/path/to/data/folder",
    "frame_rate": 25,
    "vertical_sync": true,
    "threshold": 64,
    "camera": {
    "device_id": 0,
    "width": 640,
    "height": 480,
    "rotate_180": false,
    "use_uvc": false
    },
    "background_color": {
    "red": 64,
    "green": 64,
    "blue": 64,
    "alpha": 255
    },
    "tool_highlight_color": {
    "red": 240,
    "green": 64,
    "blue": 64,
    "alpha": 128
    }
    }


  • Lifetime Qt Champion

    Then I see your problem:

    @QJsonArray jsonArray = jsonObject["server"].toArray();@

    jsonObject["server"] does not return an array



  • Your a legend, I had tried different objects earlier and they had not worked but I must had another problem that was causing me trouble.

    This works for me now:
    @
    QJsonObject jsonObject = jsonDoc.object();
    qDebug() << jsonObject.value(QString("osc_port"));
    @

    Again, many thanks for pointing out the error of my ways.

    Is there a way for me to close this question as solved or award you a +1 for helping resolved my issue ?
    Dave


  • Lifetime Qt Champion

    You're welcome !

    The only way to do that is to edit your thread title prepending [solved] (not a real closing but it gives hint to other users that a solution has been found)

    There's currently no answer ranking system (could be useful though)

    Happy coding ! :)


Log in to reply