Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved]Problems parsing a .json (resulting QJsonDocument always empty/Null)
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
22 Posts 3 Posters 16.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fharjes
    wrote on last edited by
    #3

    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).

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #4

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

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fharjes
        wrote on last edited by
        #5

        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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #6

          Can you share an example of json that is failing ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fharjes
            wrote on last edited by
            #7

            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
            }
            }
            @

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fharjes
              wrote on last edited by
              #8

              No idea? Anyone?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #9

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  fharjes
                  wrote on last edited by
                  #10

                  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!

                  1 Reply Last reply
                  0
                  • JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #11

                    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();
                    @

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      fharjes
                      wrote on last edited by
                      #12

                      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"
                      @

                      1 Reply Last reply
                      0
                      • JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on last edited by
                        #13

                        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?

                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                        1 Reply Last reply
                        0
                        • F Offline
                          F Offline
                          fharjes
                          wrote on last edited by
                          #14

                          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.

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #15

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

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • JKSHJ Offline
                              JKSHJ Offline
                              JKSH
                              Moderators
                              wrote on last edited by
                              #16

                              [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)

                              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                              1 Reply Last reply
                              0
                              • F Offline
                                F Offline
                                fharjes
                                wrote on last edited by
                                #17

                                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!

                                1 Reply Last reply
                                0
                                • JKSHJ Offline
                                  JKSHJ Offline
                                  JKSH
                                  Moderators
                                  wrote on last edited by
                                  #18

                                  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;
                                  @

                                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                  1 Reply Last reply
                                  0
                                  • F Offline
                                    F Offline
                                    fharjes
                                    wrote on last edited by
                                    #19

                                    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";
                                    ...
                                    @

                                    1 Reply Last reply
                                    0
                                    • JKSHJ Offline
                                      JKSHJ Offline
                                      JKSH
                                      Moderators
                                      wrote on last edited by
                                      #20

                                      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.

                                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                      1 Reply Last reply
                                      0
                                      • F Offline
                                        F Offline
                                        fharjes
                                        wrote on last edited by
                                        #21

                                        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!

                                        1 Reply Last reply
                                        0
                                        • JKSHJ Offline
                                          JKSHJ Offline
                                          JKSH
                                          Moderators
                                          wrote on last edited by
                                          #22

                                          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.

                                          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved