[solved]Problems parsing a .json (resulting QJsonDocument always empty/Null)



  • I have spend the last two days trying to parse a json-file into a valid QJsonDocument. The process starts, when a corresponding button is clicked. The file to be opened is given by a QLineEdit. What am I doing wrong? This is my code:

    @
    void MainWindow::on_btnOpenJSON_clicked(){

    QFile tmp;
    QJsonParseError *error = new QJsonParseError();
    tmp.setFileName(ui->txtJSonFile->text());

    if (tmp.exists()){
    tmp.open(QIODevice::ReadOnly | QIODevice::Text);
    mJsonFile = QJsonDocument::fromJson(tmp.readAll(),error);
    tmp.close();
    }
    else{
    qDebug() << "Error: File not found!";
    }

    if (mJsonFile.isNull() || mJsonFile.isEmpty()){
        qDebug() << "Error: " << error->errorString() << error->offset;
        }
    else{
        qDebug() << "File" << ui->txtJSonFile->text() << " has been parsed correctly.";
        }
    

    }
    @

    Edit: I tried several (definitely correct) json-files. I further generated a json in qt and wrote it into a file. I was not able to parse it again.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What error are you getting ?

    What does error contain ?

    Are you sure you have the rights to open and read the file ?



  • Hi and thank you for your fast answer!

    The error given/content ofQJsonParseError is: "illegal value" 0. That sounds strange to me, as I am able to give out the content of the file to be parsed by

    @
    qDebug() << tmp.readAll();
    @

    and it looks correct to me. This is the case for all files that I have tried. Regarding the rights, I have full access to the files, as they are located in my personal folder (windows 7).


  • Moderators

    Hi, pass your file's contents into something like "JSONLint":http://jsonlint.com/ . Does it pass the test?



  • Yes, the files pass the test. Meanwhile, I tried to parse the files on my Macbook at home. The result is the same. I have no idea, what could be wrong.


  • Lifetime Qt Champion

    Can you share an example of json that is failing ?



  • No Problem! One example, from the German Wikipedia article about Json, is this:

    @
    {
    "Herausgeber": "Xema",
    "Nummer": "1234-5678-9012-3456",
    "Deckung": 2e+6,
    "Waehrung": "EURO",
    "Inhaber": {
    "Name": "Mustermann",
    "Vorname": "Max",
    "maennlich": true,
    "Hobbys": [ "Reiten", "Golfen", "Lesen" ],
    "Alter": 42,
    "Kinder": [],
    "Partner": null
    }
    }
    @



  • No idea? Anyone?


  • Lifetime Qt Champion

    Hi,

    @
    QFile tmp("test.json");
    QJsonParseError error;
    QJsonDocument mJsonFile;

    if (tmp.exists()){
            tmp.open(QIODevice::ReadOnly | QIODevice::Text);
            mJsonFile = QJsonDocument::fromJson(tmp.readAll(), &error);
            tmp.close();
            }
        else{
            qDebug() << "Error: File not found!";
        }
    
        if (mJsonFile.isNull() || mJsonFile.isEmpty()){
            qDebug() << "Error: " << error.errorString() << error.offset;
            }
        else{
            qDebug() << "File" << mJsonFile << " has been parsed correctly.";
            }
    

    @
    works correctly.

    However, in your snippet you use ui->txtJSonFile->text() but you never set anything in your txtJSonFile widget.



  • Good Morning Sgaist,

    the txtJsonFile widgeht gets its content within the program window (when the user specifies the file to open). I proofed the widget`s content via qDebug(), the processing of the entered file name is correct. Setting the filename directly within the code leads to the same result. If the code given above is correct, are there any other possible reasons for the parser to fail? A check with an HexEditor shows that all files I tried start with an "{" (Hex7B) and the encoding is UTF8, as it should be. There is also no BOM, so I am really confused!


  • Moderators

    What version of Qt are you using? Did you use an official installer or build it yourself?

    Please modify your program as follows and post the output:
    @
    tmp.open(QIODevice::ReadOnly | QIODevice::Text);
    QByteArray bytes = tmp.readAll();
    qDebug() << bytes;

    mJsonFile = QJsonDocument::fromJson(bytes,error);
    qDebug() << mJsonFile;
    qDebug() << error->errorString();
    @



  • My Qt-Version is 5.4.0 (Creator 3.30), with MinGW, and I used the official installer. The output you requested is as follows:

    @
    "{
    "version": "1.0",
    "gate": {
    "id": 29
    },
    "data": [
    {
    "address": 410,
    "group": 0,
    "type": "11:0",
    "timeStamp": "2015-01-15 10:05:11.000",
    "values": [
    100,
    4.2,
    0
    ]
    },
    {
    "address": 410,
    "group": 1,
    "type": "11:1",
    "timeStamp": "2015-01-15 10:05:11.000",
    "values": [
    -76,
    0.14,
    256,
    0,
    12,
    866,
    46241,
    0
    ]
    },
    {
    "address": 410,
    "group": 2,
    "type": "11:2",
    "timeStamp": "2015-01-15 10:05:11.000",
    "values": [
    22.1312312,
    50.1231313,
    30.4,
    0,
    6,
    1,
    1.42,
    1.04,
    0.98
    ]
    },
    {
    "address": 411,
    "group": 0,
    "type": "26:0",
    "timeStamp": "2015-01-15 10:05:11.000",
    "values": [
    100,
    4.2,
    0,
    20
    ]
    },
    {
    "address": 411,
    "group": 1,
    "type": "26:1",
    "timeStamp": "2015-01-15 10:05:11.000",
    "values": [
    22.1312312,
    50.1231313,
    30.4,
    0,
    6,
    1,
    1.42,
    1.04,
    0.98,
    12
    ]
    },
    {
    "address": 411,
    "group": 2,
    "type": "26:2",
    "timeStamp": "2015-01-15 10:05:11.000",
    "values": [
    827850752,
    73847942565529090,
    827850752,
    73847942565529090,
    827850752,
    73847942566053380,
    827850752,
    73847942566053380,
    827850752,
    73847942566577660,
    25
    ]
    }
    ]
    }"
    QJsonDocument()
    "illegal value"
    @


  • Moderators

    I also used the official Qt 5.4.0 + MinGW, on Windows 8.1. I got...
    @
    QJsonDocument({"data":[{"address":410,"group":0,"timeStamp":"2015-01-15 10:05:11.000","type":"11:0","values":[100,4.2000000000000002,0]},{"address":410,"group":1,"timeStamp":"2015-01-15 10:05:11.000","type":"11:1","values":[-76,0.14000000000000001,256,0,12,866,46241,0]},{"address":410,"group":2,"timeStamp":"2015-01-15 10:05:11.000","type":"11:2","values":[22.131231199999998,50.123131299999997,30.399999999999999,0,6,1,1.4199999999999999,1.04,0.97999999999999998]},{"address":411,"group":0,"timeStamp":"2015-01-15 10:05:11.000","type":"26:0","values":[100,4.2000000000000002,0,20]},{"address":411,"group":1,"timeStamp":"2015-01-15 10:05:11.000","type":"26:1","values":[22.131231199999998,50.123131299999997,30.399999999999999,0,6,1,1.4199999999999999,1.04,0.97999999999999998,12]},{"address":411,"group":2,"timeStamp":"2015-01-15 10:05:11.000","type":"26:2","values":[827850752,73847942565529088,827850752,73847942565529088,827850752,73847942566053376,827850752,73847942566053376,827850752,73847942566577664,25]}],"gate":{"id":29},"version":"1.0"})
    "no error occurred"
    @

    ...so something strange is going on with your machine. I've never seen this problem before.

    Could you try on a different PC?



  • Thats really strange! As I said above, I tried the same code on two machines; a macbook and a windows pc. The result was the same... I still have no Idea, what could be the reason.


  • Lifetime Qt Champion

    Then let's do some shots in the dark: Country ? File Encoding ? Codepage ?


  • Moderators

    [quote author="SGaist" date="1423948310"]Then let's do some shots in the dark: Country ? File Encoding ? Codepage ? [/quote]Good idea. Try setting your computer's Locale to an English-speaking country (e.g. USA or UK), reboot your PC, and see if the program works now. (Double-check that the file really is encoded in UTF-8)



  • I solved the problem this morning, although I am still a little bit confused. It seems that my debug output sets the file pointer to the end of the file, so that the parser considers it to be empty. That makes sense, but I had the same mistake before I inserted the code for the output. And, finally, the reason to have the output was the parser error.... nevertheless, thank you very much for your help and your patience!


  • Moderators

    I'm a bit confused.

    Earlier, you said that the following code outputted the full document (which means your file pointer was ok)...
    @
    QByteArray bytes = tmp.readAll();
    qDebug() << bytes;
    @

    ...but the following code produces emptiness, even though bytes already contains the full document?
    @
    mJsonFile = QJsonDocument::fromJson(bytes,error);
    qDebug() << mJsonFile;
    @



  • Unfortunately, I was stupid enough to set another output directly after the file is opened. Something like this:

    @
    ...
    if (f.open(QIODevice::ReadOnly)) {
    qDebug() << "File opened";
    qDebug() << f.readAll(); //Stupid!
    QByteArray data = f.readAll();
    QJsonParseError error;
    QJsonDocument json(QJsonDocument::fromJson(data));
    qDebug() << json; // Thats ok!
    qDebug() << error.error << error.errorString();
    qDebug() << json.object();
    qDebug() << json.isNull() << json.isEmpty();
    } else {
    qDebug() << "could not open File!";
    }
    } else {
    qDebug() << "Error file does not exist";
    ...
    @


  • Moderators

    Yeah, calling readAll() sets the pointer at the end of the file. You can't read it twice without resetting the QFile.

    But anyway, it's good that you've found the solution :) Happy coding!

    P.S. In the future, please post the code that you actually used, and please try the sample code that we post for you without changing it (unless the sample is wrong). That will prevent confusions like this in the future.



  • It was not my intention to confuse you, but my code changed over the days as I continued the search for the solution of my problem. Therefore, the samples here were not up to date from time to time. Sorry again!


  • Moderators

    It's ok :)

    [quote author="fharjes" date="1424158467"]Therefore, the samples here were not up to date from time to time.[/quote]I meant my samples -- The output that you posted on 13 Feb was obtained using different code from what I posted.

    So what I'm trying to say is: When others give you code in order to check output, run that code unmodified. Otherwise, your output might be wrong.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.