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. Getting a JSON string from PHP
Forum Updated to NodeBB v4.3 + New Features

Getting a JSON string from PHP

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 3.8k Views 2 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.
  • K Offline
    K Offline
    Krag
    wrote on 14 Nov 2015, 11:32 last edited by
    #1

    I can't seem to covert a JSON string.
    I am getting my JSON string from a PHP file.
    Here's my code:

    void MainWindow::replyFinished(QNetworkReply *reply)
    {
    QMessageBox::information(this, "status", reply->readAll()); //Outputs a correct JSON string
    qDebug() << reply->readAll(); //Outputs an empty string
    QByteArray jsonData = reply->readAll();
    if (jsonData.isEmpty()) {
    qDebug() << "Empty"; //This output is executed
    }
    else
    qDebug() << jsonData;

     QJsonDocument doc;
     QJsonParseError parseError;
     doc.fromJson(jsonData, &parseError);
          qDebug() << parseError.error; //Error outputs: 5
    

    }

    I guess reply->readAll() is wrong since QDebug doesn't work with it. What should I use instead?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 14 Nov 2015, 12:22 last edited by
      #2

      Hi, welcome to devnet.

      Your problem is calling readAll() repeatedly. The reply object is a stream. Once you readAll() from it it's empty. So read it once to some byte array and use that instead of calling readAll() again, as there's nothing left to read.

      Also, you should call reply->deleteLater() after you're done with the reply, because you're leaking memory.

      K 1 Reply Last reply 14 Nov 2015, 18:43
      0
      • C Chris Kawa
        14 Nov 2015, 12:22

        Hi, welcome to devnet.

        Your problem is calling readAll() repeatedly. The reply object is a stream. Once you readAll() from it it's empty. So read it once to some byte array and use that instead of calling readAll() again, as there's nothing left to read.

        Also, you should call reply->deleteLater() after you're done with the reply, because you're leaking memory.

        K Offline
        K Offline
        Krag
        wrote on 14 Nov 2015, 18:43 last edited by
        #3

        @Chris-Kawa thanks for replying. I stored the reply->readAll() in a QByteArray but now I am facing another problem. I cannot convert the JSON string to an object.

        Here's my code:

        void MainWindow::replyFinished(QNetworkReply *reply)
        {
        QByteArray jsonData = reply->readAll();
        if (jsonData.isEmpty()) {
        qDebug() << "Empty";
        }
        else
        qDebug() << jsonData;

         QJsonDocument doc;
         QJsonParseError parseError;
         doc.fromJson(jsonData, &parseError);
              qDebug() << parseError.error;
        
             if (!doc.isObject()) {
                 qDebug() << "Document is not an object"; //This is executed.
             }
        
             QJsonObject object = doc.object();
             QJsonValue jsonValue = object.value("players");
        

        }

        The JSON string that my PHP file outputs looks like this:
        [{"players":"1","lastUpdate":"1446911197"}]

        However, QByteArray is:
        "[{"players":"1","lastUpdate":"1446911197"}] \n"

        Is the "\n" normal?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 14 Nov 2015, 21:19 last edited by
          #4

          Hi and welcome to devnet,

          fromJson is a static function returning a QJsonDocument, you're using it wrongly. Currently your doc object is just empty.

          Note that the json you have doesn't contain an object but an array.

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

          K 1 Reply Last reply 15 Nov 2015, 18:37
          0
          • S SGaist
            14 Nov 2015, 21:19

            Hi and welcome to devnet,

            fromJson is a static function returning a QJsonDocument, you're using it wrongly. Currently your doc object is just empty.

            Note that the json you have doesn't contain an object but an array.

            K Offline
            K Offline
            Krag
            wrote on 15 Nov 2015, 18:37 last edited by
            #5

            @SGaist said:

            hat the js

            Okay, I used it correctly and made myself a QJsonArray. But what do I do now? How do I get values from it?

             QJsonParseError parseError;
             QJsonDocument doc = QJsonDocument::fromJson(jsonData, &parseError);
                 qDebug() << parseError.error;
                 QJsonArray theArray = doc.array();
            
            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 15 Nov 2015, 22:06 last edited by
              #6

              You have an array of one element so your can do something like:

              QJsonArray array = jsondoc.array();
              QVariant firstElement = array.at(0).toVariant();
              QVariantMap variantMap = firstElement.toMap();
              int players = variantMap["players"].toInt();
              

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

              K 1 Reply Last reply 16 Nov 2015, 14:10
              1
              • S SGaist
                15 Nov 2015, 22:06

                You have an array of one element so your can do something like:

                QJsonArray array = jsondoc.array();
                QVariant firstElement = array.at(0).toVariant();
                QVariantMap variantMap = firstElement.toMap();
                int players = variantMap["players"].toInt();
                
                K Offline
                K Offline
                Krag
                wrote on 16 Nov 2015, 14:10 last edited by
                #7

                @SGaist Thank you, it worked :)

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 16 Nov 2015, 20:52 last edited by
                  #8

                  You're welcome !

                  Since it's working now, please mark the thread as solved using the "Topic Tool" button so other forum users may know a solution has been found :)

                  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

                  1/8

                  14 Nov 2015, 11:32

                  • Login

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