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. Need example for reading/writing json files
Forum Updated to NodeBB v4.3 + New Features

Need example for reading/writing json files

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 3.7k 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.
  • M Offline
    M Offline
    Muzab
    wrote on 22 Mar 2016, 16:54 last edited by Muzab
    #1

    Hello everybody!

    Qt has excellent documentation. Sometimes elaborate examples can save us time.
    i am following this link to understand how to read/parse json from a website. This code works for some websites but not for some ( can someone explain why this might be happening?). Plus how to convert Json object to array or rather how do you parse data in Qt for Json files?

    connect(&qnam,SIGNAL(finished(QNetworkReply*)),this,SLOT(onFinished(QNetworkReply*)));
        QUrl url = QString::fromLocal8Bit("http://thethingsnetwork.org/api/v0/nodes/05060708/");
        QNetworkRequest request(url);
        m_qnam.get(request);
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    void Dialog::onFinished(QNetworkReply* response)
    {
        QVariant redirectionTarget = response->attribute(QNetworkRequest::RedirectionTargetAttribute);
        qDebug() << redirectionTarget;
        if (!redirectionTarget.isNull()) {
                QUrl newUrl = QUrl(redirectionTarget.toUrl());
                QNetworkRequest request(newUrl);
                qnam.get(request);
        } else {
            byteResponse = (QByteArray)response->readAll();       
           jsonResponse = QJsonDocument::fromJson(byteResponse);
    
           if(jsonResponse.isObject())
           {
               jsonObject = jsonResponse.object();
               qDebug() <<"document is an object";
    
    /* HOW DO YOU CONVERT jSon Object to Json array and ??????*/
       
               }
           }
           if(jsonResponse.isArray())
           {
                jsonArray = jsonResponse.array();
                qDebug() <<"document is an Array";
    
    /* IF DATA IS ARRAY .....THIS IS NOT WORKING */
    
                foreach(const QJsonValue &value, jsonArray)
                {
                    QJsonObject obj = value.toObject();
                   data.append(obj["data_raw"].toString());
                    start_Time.append(obj["time"].toString());
    
                    qDebug()<<data;
                }
            }
        }
        response->deleteLater();
    }
    
    
    1 Reply Last reply
    0
    • P Offline
      P Offline
      Pablo J. Rogina
      wrote on 22 Mar 2016, 17:36 last edited by Pablo J. Rogina
      #2

      @Muzab

      This code works for some websites but not for some

      Could you be more specific about details? Some URLs for sites not working?
      I tried the URL from you code sample ("http://kiflab.com/jsonfile") and I got a 404 error, no JSON file at all so at that point Qt is out of the equation.

      In addition, I'd sepatate the JSON processing from the network connections, so could you try processing a JSON file (gathered from some of the sites "not working", I mean, I'd connect and save the JSON data by hand into a file and then I'd feed it to the JSON object)

      Also, have you played with this example?

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Muzab
        wrote on 23 Mar 2016, 10:21 last edited by
        #3

        Hi Pablo I have updated the weblink that I want to read data from but I cannot read the contents of the array to a QByteArray. But I can for this link "http://thethingsnetwork.org/api/v0/".
        It is on the same server.
        Thanks for pointing out the example. I will test it out.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Muzab
          wrote on 23 Mar 2016, 10:57 last edited by
          #4

          Sorry guys! My code works, just website was wrong

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Pablo J. Rogina
            wrote on 23 Mar 2016, 13:02 last edited by
            #5

            @Muzab I'm glad you solved your problem. I was about to suggest this website JSONLint an online JSON validation tool. I use it frequently to be sure the JSON data I'm handling is valid, so the parsing code I write should be as well.

            Please don't forget to mark this post as solved. Thanks.

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0

            1/5

            22 Mar 2016, 16:54

            • Login

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