Error when reading Json File
-
Hi everyone. I want to read a json file. It gives me an error in there:
How can i fix it```
if(json_error.error != QJsonParseError::NoError)
{
qDebug() << "json error!";
return 1;
}#include <QFile>
#include <QDebug>
#include <QJsonParseError>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>int main()
{QFile loadFile("/home/a/QT Projects/JsonDeneme2/fruit.json"); if(!loadFile.open(QIODevice::ReadOnly)) { qDebug() << "could't open projects json"; return 1; } QByteArray allData = loadFile.readAll(); loadFile.close(); QJsonParseError json_error; QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error)); if(json_error.error != QJsonParseError::NoError) { qDebug() << "json error!"; return 1; } QJsonObject rootObj = jsonDoc.object(); QStringList keys = rootObj.keys(); for(int i = 0; i < keys.size(); i++) { qDebug() << "key" << i << " is:" << keys.at(i); } //Because it is a predefined JSON data format, it can be read like this here. if(rootObj.contains("first fruit")) { QJsonObject subObj = rootObj.value("first fruit").toObject(); qDebug() << "describe is:" << subObj["describe"].toString(); qDebug() << "icon is:" << subObj["icon"].toString(); qDebug() << "name is:" << subObj["name"].toString(); } if(rootObj.contains("second fruit")) { QJsonObject subObj = rootObj.value("second fruit").toObject(); qDebug() << "describe is:" << subObj.value("describe").toString(); qDebug() << "icon is:" << subObj.value("icon").toString(); qDebug() << "name is:" << subObj.value("name").toString(); } if(rootObj.contains("third fruit array")) { QJsonArray subArray = rootObj.value("three fruit array").toArray(); for(int i = 0; i< subArray.size(); i++) { qDebug() << i<<" value is:" << subArray.at(i).toString(); } }
}
-
Don't know how often we must tell you to use code tags to make your code readable!
-
You should make sure it really is valid json. I'd run the file through jsonlint.com and it will tell you where the error is.
-
@mranger90 thank you. My json was wrong
-
@suslucoder If you want to get help then provide needed information: you did not post the actuall error message and you did not post an example of your JSON file you're trying to parse! And you really expect to get help? I will simply ignore such posts in the future...
-
@jsulm Because i didnt get any error message. I know is there a mistake because i've added this part to my code.
if(json_error.error != QJsonParseError::NoError) { qDebug() << "json error!"; return 1; }
I thought, I was wrong the parsing part. If i think that my JSON is wrong, i would share it.
-
Still no code tags... -
@suslucoder said in Error when reading Json File:
qDebug() << "json error!";
I suggest you look at the docs of QJsonParseError Struct and use that in your code to extract much more meaningful information than this!
-
@suslucoder said in Error when reading Json File:
In addition to all the previous requests and suggestions, you may also want to learn about the capabilities of the Qt framework, and its different classes.And by reading the documentation of QJsonParseError you'll see that error is a enumeration that will provide the type of error while parsing JSON.
Moreover, QJsonParseError::errorString() method will provide "the human-readable message appropriate to the reported JSON parsing error"
-
@Christian-Ehrlicher I did use it. It looks in a code tag in my screen.