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