|HELP| : How to extract Data from Json file ?
-
I have a Json file wich has the format starting with a "[" , and has objects inside it, each object has its own attributes. My purpose is to retrieve these data of each object and insert it in a database, that's another talk but for the moment I want just to print them. I was struggling with this for about 4 days without finding what I need.
here is an example of my file :[ { "updated":1592183850402, "country":"Morocco", "countryInfo": {"_id":504, "iso2":"MA", "iso3":"MAR", "lat":32, "continent":"Africa", "activePerOneMillion":22.12 } }, { "updated":159388477402, "country":"France", "countryInfo": {"_id":504, "iso2":"FR", "iso3":"FRA", "lat":332, "continent":"Europe", "activePerOneMillion":66.12 } } ]
And I really need help, since I'm new to both Qt and Json I can't go so far by myself. if any one can help me with a piece of code.
this is what I wrote, but whenever I reache the foreach loop I can't figure out my next move. I have tried different ways inside the loop but none of it worked.QTextStream file_text(&my_json); QString json_string; json_string = file_text.readAll(); my_json.close(); QByteArray json_bytes = json_string.toLocal8Bit(); auto json_doc = QJsonDocument::fromJson(json_bytes); QJsonObject json_obj = json_doc.object(); QJsonArray jsonArray = json_obj["activePerOneMillion"].toArray(); foreach (const QJsonValue & value, jsonArray) { ... }
-
Thank you all. And a special thanks to @LeLev who helped me in private. apparently the base-ranged for loop helped me since it reconizes the type of objects that are stored in the QjsonArray so it access them all. This was what worked for me:
QFile countries_file("countries.json"); QFile morocco_file("morocco.json"); QString json_string; if(countries_file.open(QIODevice::ReadOnly | QIODevice::Text)){ json_string = countries_file.readAll(); countries_file.close(); } else qDebug()<< "file not found"; auto json_doc = QJsonDocument::fromJson(json_string.toUtf8()); QJsonArray jArr = json_doc.array(); QJsonValue val; for(auto jsonObj : jArr) { val = jsonObj.toObject().value("country"); _country = val.toString(); qDebug() << "Country: "<< _country; val = jsonObj.toObject().value("cases"); _cases = val.toInt(); qDebug() << "Cases: "<< _cases; val = jsonObj.toObject().value("deaths"); _deaths = val.toInt(); qDebug() << "Deaths: "<< _deaths; } qDebug() << Qt::endl; qDebug() << Qt::endl;
-
Hi and welcome to the forums.
You seem off to a good start.
What you have is a json array with objects in it.So in you for loop you will get QJsonObject and not a
QJsonValue if i read it correctly :=)
Nope. @JonB is right :) -
@Elmehdi said in |HELP| : How to extract Data from Json file ?:
I'm new to both Qt and Json
Just in case, have you read the documentation?
There's even a good example...
-
@Elmehdi
With all die respect to my learned friend @mrjj, you do getQJsonValue
s as you iterate throughQJsonArray jsonArray
.The point is, a
QJsonValue
can hold many data types (see https://doc.qt.io/qt-5/qjsonvalue.html#details). In your case those will beQJsonObject
s. The first thing you should write in your code is:foreach (const QJsonValue & value, jsonArray) { qDebug() << value.type(); }
and look at the values per https://doc.qt.io/qt-5/qjsonvalue.html#Type-enum.
From there, you will probably also want to look at
QJsonObject QJsonValue::toObject()
and/orQVariant QJsonValue::toVariant()
.If you know about recursion, that will be pretty handy when parsing JSON data.
-
Hi
I think what confused me is this
QJsonArray jsonArray = json_obj["cases"].toArray();
i dont see "cases" in the data sample. -
Thank you all. And a special thanks to @LeLev who helped me in private. apparently the base-ranged for loop helped me since it reconizes the type of objects that are stored in the QjsonArray so it access them all. This was what worked for me:
QFile countries_file("countries.json"); QFile morocco_file("morocco.json"); QString json_string; if(countries_file.open(QIODevice::ReadOnly | QIODevice::Text)){ json_string = countries_file.readAll(); countries_file.close(); } else qDebug()<< "file not found"; auto json_doc = QJsonDocument::fromJson(json_string.toUtf8()); QJsonArray jArr = json_doc.array(); QJsonValue val; for(auto jsonObj : jArr) { val = jsonObj.toObject().value("country"); _country = val.toString(); qDebug() << "Country: "<< _country; val = jsonObj.toObject().value("cases"); _cases = val.toInt(); qDebug() << "Cases: "<< _cases; val = jsonObj.toObject().value("deaths"); _deaths = val.toInt(); qDebug() << "Deaths: "<< _deaths; } qDebug() << Qt::endl; qDebug() << Qt::endl;