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. Can't convert QJsonValue to any type
Qt 6.11 is out! See what's new in the release blog

Can't convert QJsonValue to any type

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 5.0k 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.
  • T Offline
    T Offline
    TheMushroom
    wrote on last edited by
    #1

    I'm having a weird problem where I can't seem to convert a QJsonValue to any of the possible values.

    The piece of JSON data I'm trying to parse is this:

    }
        "payload":{
            "orders":[
                {
                    "last_update":"2018-08-28T16:09:01+00:00",
                    "platinum":11,
                    "creation_date":"2015-12-24T14:20:21+00:00",
                    "id":"567bff25cbfa8f06d7e8290b",
                    "platform":"pc",
                    "quantity":27,
                    "region":"en",
                    "order_type":"sell",
                    "visible":true
                },
                {
                    "last_update":"2018-08-29T14:38:35+00:00",
                    "platinum":75,
                    "creation_date":"2016-01-11T12:42:42+00:00",
                    "id":"5693a342cbfa8f263aeb7d02",
                    "platform":"pc",
                    "quantity":1,
                    "region":"en",
                    "order_type":"sell",
                    "visible":true
                },
                {
                    "last_update":"2018-08-18T11:03:56+00:00",
                    "platinum":35,
                    "creation_date":"2016-06-11T18:38:37+00:00",
                    "id":"575c5aadd3ffb658a3916831",
                    "platform":"pc",
                    "quantity":1,
                    "region":"en",
                    "order_type":"sell",
                    "visible":true
                }
    	]
        }
    }
    

    Like you can see, the "orders" value is an Array.

    But when I try to convert it into a QJsonArray, it gives me an array with a length of 0:

    // Converts each order in the JSON data into a C++ data structure and returns all of them in a QList.
    QList<WFMOrder> WFMOrder::fromFullJson(const QJsonDocument& json)
    {
        QList<WFMOrder> orders;
    
        QJsonValue value = json["payload"]["orders"];
        qDebug() << "isArray = " << value.isArray(); // This is false
        qDebug() << "isObject = " << value.isObject(); // This is false
        qDebug() << "isString = " << value.isString(); // This is false
        qDebug() << "isNull = " << value.isNull(); // This is false
        qDebug() << "isBool = " << value.isBool(); // This is false
        qDebug() << "isDouble = " << value.isDouble(); // This is false
        qDebug() << "Object count in orders: " << value.toArray().count(); // This is 0
        for (int i = 0; i < value.toArray().count(); i++) // This runs 0 times.
        {
            WFMOrder order(json, i);
            orders.append(order);
        }
    
        return orders; // The length of the returned QList is also 0
    }
    

    I'm a little confused, what am I doing wrong?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      Are you sure that syntax works ?
      QJsonValue value = json["payload"]["orders"];
      since "payload": is object not array.
      Anyway i would try to call
      http://doc.qt.io/qt-5/qjsonvalue.html#isArray
      on value to see what it says.

      T 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        Are you sure that syntax works ?
        QJsonValue value = json["payload"]["orders"];
        since "payload": is object not array.
        Anyway i would try to call
        http://doc.qt.io/qt-5/qjsonvalue.html#isArray
        on value to see what it says.

        T Offline
        T Offline
        TheMushroom
        wrote on last edited by
        #3

        @mrjj Calling isArray, isBool, isObject, isDouble, isString and isNull on value all returns false.
        I believe the syntax does work since I can do this value[0]["platinum"].toInt() which returns 11, and that matches what the JSON data has in the platinum field.

        mrjjM 1 Reply Last reply
        0
        • T TheMushroom

          @mrjj Calling isArray, isBool, isObject, isDouble, isString and isNull on value all returns false.
          I believe the syntax does work since I can do this value[0]["platinum"].toInt() which returns 11, and that matches what the JSON data has in the platinum field.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @TheMushroom
          Ah sorry i missed u already called all the isX

          hmm if
          value[0]["platinum"].toInt()
          works then syntax is ok. ( didnt know. cool)
          but that implies that value is in fact an array
          but at same time value.isArray(); returns false?

          i wondering if u need to call
          QJsonValue::toArray()

          T 1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by A Former User
            #5

            This will help you out : https://stackoverflow.com/questions/51728253/how-to-parse-json-array-object

            1 Reply Last reply
            0
            • mrjjM mrjj

              @TheMushroom
              Ah sorry i missed u already called all the isX

              hmm if
              value[0]["platinum"].toInt()
              works then syntax is ok. ( didnt know. cool)
              but that implies that value is in fact an array
              but at same time value.isArray(); returns false?

              i wondering if u need to call
              QJsonValue::toArray()

              T Offline
              T Offline
              TheMushroom
              wrote on last edited by TheMushroom
              #6

              @mrjj I tried value.toArray() as well, but it also produced an empty array.

              @VInay123 I can't seem to loop the QJsonObject like they do in the stackoverflow post, it gives me this error:
              non-const lvalue reference to type 'QJsonValueRef' cannot bind to a temporary of type 'QJsonValueRef'

              QJsonObject obj = json["payload"]["orders"].toObject();
              for (auto& order : obj) // non-const lvalue reference to type 'QJsonValueRef' cannot bind to a temporary of type 'QJsonValueRef'
              {
                      
              }
              
              ? 1 Reply Last reply
              0
              • T TheMushroom

                @mrjj I tried value.toArray() as well, but it also produced an empty array.

                @VInay123 I can't seem to loop the QJsonObject like they do in the stackoverflow post, it gives me this error:
                non-const lvalue reference to type 'QJsonValueRef' cannot bind to a temporary of type 'QJsonValueRef'

                QJsonObject obj = json["payload"]["orders"].toObject();
                for (auto& order : obj) // non-const lvalue reference to type 'QJsonValueRef' cannot bind to a temporary of type 'QJsonValueRef'
                {
                        
                }
                
                ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                @TheMushroom Which compiler are you using? I think in VS it must work.

                T 1 Reply Last reply
                0
                • ? A Former User

                  @TheMushroom Which compiler are you using? I think in VS it must work.

                  T Offline
                  T Offline
                  TheMushroom
                  wrote on last edited by
                  #8

                  @VInay123 I'm using the MinGW compiler.

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @TheMushroom said in Can't convert QJsonValue to any type:

                    for (auto& order : obj)

                    you're missing the const:
                    --> for (const auto & order : obj)

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    T 1 Reply Last reply
                    2
                    • Christian EhrlicherC Christian Ehrlicher

                      @TheMushroom said in Can't convert QJsonValue to any type:

                      for (auto& order : obj)

                      you're missing the const:
                      --> for (const auto & order : obj)

                      T Offline
                      T Offline
                      TheMushroom
                      wrote on last edited by TheMushroom
                      #10

                      @Christian-Ehrlicher That fixed the error but it doesn't loop at all. Added qDebug() << "Looped"; inside it, which didn't run a single time.

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Why not simply writing a small testcase?

                        const QByteArray jsonBa = R"raw(
                        {
                          "payload":{
                                "orders":[
                                    {
                                        "last_update":"2018-08-28T16:09:01+00:00",
                                        "platinum":11,
                                        "creation_date":"2015-12-24T14:20:21+00:00",
                                        "id":"567bff25cbfa8f06d7e8290b",
                                        "platform":"pc",
                                        "quantity":27,
                                        "region":"en",
                                        "order_type":"sell",
                                        "visible":true
                                    },
                                    {
                                        "last_update":"2018-08-29T14:38:35+00:00",
                                        "platinum":75,
                                        "creation_date":"2016-01-11T12:42:42+00:00",
                                        "id":"5693a342cbfa8f263aeb7d02",
                                        "platform":"pc",
                                        "quantity":1,
                                        "region":"en",
                                        "order_type":"sell",
                                        "visible":true
                                    },
                                    {
                                        "last_update":"2018-08-18T11:03:56+00:00",
                                        "platinum":35,
                                        "creation_date":"2016-06-11T18:38:37+00:00",
                                        "id":"575c5aadd3ffb658a3916831",
                                        "platform":"pc",
                                        "quantity":1,
                                        "region":"en",
                                        "order_type":"sell",
                                        "visible":true
                                    }
                        	]
                          }
                        })raw";
                        
                        QJsonParseError err;
                        QJsonDocument doc(QJsonDocument::fromJson(jsonBa, &err));
                        printf("err: %s %d\n", qPrintable(err.errorString()), err.offset);
                        QJsonObject obj = doc.object();
                        printf("obj is empty: %d\n", obj.isEmpty());
                        QJsonObject payload = obj["payload"].toObject();
                        printf("payload is empty: %d, orders is array: %d\n", payload.isEmpty(), payload["orders"].isArray());
                        QJsonArray arr = payload["orders"].toArray();
                        for (int i = 0; i < arr.size(); ++i)
                          printf("%d\n", i);
                        

                        Works perfectly fine for me ...

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        T 1 Reply Last reply
                        1
                        • Christian EhrlicherC Christian Ehrlicher

                          Why not simply writing a small testcase?

                          const QByteArray jsonBa = R"raw(
                          {
                            "payload":{
                                  "orders":[
                                      {
                                          "last_update":"2018-08-28T16:09:01+00:00",
                                          "platinum":11,
                                          "creation_date":"2015-12-24T14:20:21+00:00",
                                          "id":"567bff25cbfa8f06d7e8290b",
                                          "platform":"pc",
                                          "quantity":27,
                                          "region":"en",
                                          "order_type":"sell",
                                          "visible":true
                                      },
                                      {
                                          "last_update":"2018-08-29T14:38:35+00:00",
                                          "platinum":75,
                                          "creation_date":"2016-01-11T12:42:42+00:00",
                                          "id":"5693a342cbfa8f263aeb7d02",
                                          "platform":"pc",
                                          "quantity":1,
                                          "region":"en",
                                          "order_type":"sell",
                                          "visible":true
                                      },
                                      {
                                          "last_update":"2018-08-18T11:03:56+00:00",
                                          "platinum":35,
                                          "creation_date":"2016-06-11T18:38:37+00:00",
                                          "id":"575c5aadd3ffb658a3916831",
                                          "platform":"pc",
                                          "quantity":1,
                                          "region":"en",
                                          "order_type":"sell",
                                          "visible":true
                                      }
                          	]
                            }
                          })raw";
                          
                          QJsonParseError err;
                          QJsonDocument doc(QJsonDocument::fromJson(jsonBa, &err));
                          printf("err: %s %d\n", qPrintable(err.errorString()), err.offset);
                          QJsonObject obj = doc.object();
                          printf("obj is empty: %d\n", obj.isEmpty());
                          QJsonObject payload = obj["payload"].toObject();
                          printf("payload is empty: %d, orders is array: %d\n", payload.isEmpty(), payload["orders"].isArray());
                          QJsonArray arr = payload["orders"].toArray();
                          for (int i = 0; i < arr.size(); ++i)
                            printf("%d\n", i);
                          

                          Works perfectly fine for me ...

                          T Offline
                          T Offline
                          TheMushroom
                          wrote on last edited by TheMushroom
                          #12

                          @Christian-Ehrlicher I actually did write many different tests, however none of them worked.

                          I managed to fix the issue though.

                          The actual problem was that I sent empty data to the QJsonDocument, and obviously that doesn't work.
                          The data actually comes from a QNetworkReply, and apparently calling readAll() actually removes the data located in the reply, so calling readAll() twice results in it returning an empty array the second time. (which is what I did)
                          I then passed that empty array to QJsonDocument, which was passed to the function.

                          I said earlier that using value[0]["platinum"].toInt() worked. Turns out it actually didn't.
                          What I actually did when I thought it worked was this:

                          qDebug() << "Platinum: " << value[0]["platinum"].toInt(11);
                          

                          For some reason I decided to put 11 as a default value in toInt() (I'm not sure what I was thinking when I did that, and I have no idea how I didn't notice it until now). So no matter if it succeeded or failed it would always print the seemingly valid value 11 to the console (which matched the value in the Json data), which tricked me into thinking that the QJsonDocument was actually valid, when it in reality was not.

                          1 Reply Last reply
                          1
                          • Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by Christian Ehrlicher
                            #13

                            That's why there is QJsonParseError as shown in my example :)

                            /edit: please mark the thread as solved if all works now as expected.

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            1 Reply Last reply
                            3

                            • Login

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