3

I've got a trouble while parsing JSON using QJON objects.

I read a json file of mine referenced in a resource file, read the content and try to initialize a QJSONDocument from the QString I got. And it seems it's not working

Here is the code I use :

QFile myFile(":/mime/iconemapping.json");

myFile.open(QIODevice::ReadOnly);

QJsonDocument jsonContent;
QJsonObject root;
QString jsonString = QString::fromUtf8(myFile.readAll()).simplified();

jsonContent = QJsonDocument::fromJson(jsonString.toUtf8());

root = jsonContent.object();

QJsonObject ext = root["extensions"].toObject();

QStringList listeCle = ext.keys();
int s = listeCle.size();

for (int i = 0; i < listeCle.size(); i++) {
    QString cle = listeCle.at(i).toLocal8Bit().constData();
    MAP_ICONE_MIME.insert(cle, ext[cle].toString());
}

myFile.close();

Before I try QJSONDocument::fromJson() my jsonString contains : { "extensions" : { ".7z" : ":/mime/7zip.png", ".ace" : ":/mime/ace.png", ".ai" : ":/mime/ai.png", ".eps" : ":/mime/ai.png", ".alg" : ":/mime/algobox.png", ".rar" : ":/mime/archive.png", ".aiff" : ":/mime/audio-x-generic.png"}. (there is more data but I think you get it).

The program doesn't stop unexpectedly but listeCle.size() is always 0. I tried to access directly to ext[".7z"].toString() but I still get "" as a result.

I probably made an enormous mistake, but until now that's the only JSON parsing that fails in the program.

Would you have any explanation or clue ?

Thank you for everything

Iuliu
  • 4,001
  • 19
  • 31
Marc Delerue
  • 65
  • 10
  • I think your json is not valid, you need a `}` at the end. Is this a copy/paste error or is this really the json you're using? – Iuliu Oct 30 '14 at 09:47
  • Can I say I hate myself and i wanna die ? It is a copy paste error but from my old project to the new one. And I'm stuck on it for 3 hours :( Sorry for the waste of time – Marc Delerue Oct 30 '14 at 09:51
  • Chill, everyone does mistakes like these sometimes... – Iuliu Oct 30 '14 at 10:12

1 Answers1

1

So the JSON was not valid. I recommend using http://jsonformatter.curiousconcept.com/ in the future, it's a great website.

{
    "extensions":{
        ".7z":":/mime/7zip.png",
        ".ace":":/mime/ace.png",
        ".ai":":/mime/ai.png",
        ".eps":":/mime/ai.png",
        ".alg":":/mime/algobox.png",
        ".rar":":/mime/archive.png",
        ".aiff":":/mime/audio-x-generic.png"
    }
}
Iuliu
  • 4,001
  • 19
  • 31