Need example for reading/writing json files
-
wrote on 22 Mar 2016, 16:54 last edited by Muzab
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(); }
-
wrote on 22 Mar 2016, 17:36 last edited by Pablo J. Rogina
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?
-
wrote on 23 Mar 2016, 10:21 last edited by
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. -
wrote on 23 Mar 2016, 10:57 last edited by
Sorry guys! My code works, just website was wrong
-
wrote on 23 Mar 2016, 13:02 last edited by
1/5