Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

struggle with json object keys name and utf8



  • Hi everyone and thanks in advance for your help :)

    I am currently struggling with special characters as i am making an app for spanish clients and that involve lots of weird accents everywhere...

    So i figured out how to import my form answers as a json and read through it to get the values i want with the id i want.

    But now some of the keys for my json object contain special character and because of that it does not find it.
    Which is annoying because the key inside the json does have the accent, so as the character in my code so i guess it is during the execution that the encoding goes wrong or when i read the jsonfile maybe ?.

    Im sure the answer is simple and obvious for many of you but i just begin my c++ journey and for me everything is werid so far :)

    Here is a pieice of the code that i think should reflect the situation :

       doc=doc.fromJson(data_json.toUtf8());
        obj=doc.object();
        QJsonArray jsonArray = obj["rows"].toArray();
    
        foreach (const QJsonValue & value, jsonArray) {
            QJsonObject obj = value.toObject();
            list_item.append(obj["id"].toString());
            if(id_op==obj["id"].toString())
            {
                list_item = obj.keys();
                QString dir = "dirección";
                ui->label_A1->setText(obj["nombre"].toString());
                ui->label_A2->setText(obj["fechadenacimiento"].toString());
                ui->label_A3->setText(obj["horadenacimiento"].toString());
                ui->label_A4->setText(obj["edad"].toString());
                ui->label_A5->setText(obj["estadocivil"].toString());
                ui->label_A6->setText(obj["pesoenkg"].toString());
                ui->label_A7->setText(obj["presiónarterial"].toString());
                ui->label_A8->setText(obj[dir.toUtf8()].toString());
    
    

    and some datas form the Json file :

     "edad": "32",
          "estadocivil": "Soltero/a",
          "pesoenkg": "60",
          "presiónarterial": "",
          "dirección": "lanquin",
    

    All the keys without special characters are being retreived correctly.
    Thanks a lot

    Ps : Here is the full code if it helps

    QStringList list;
        QStringList list_item;
        QJsonObject obj;
        QJsonDocument doc;
        QString data_json;
        //QByteArray data_json;
        QFile file("csvdata.json");
        file.open(QIODevice::ReadOnly | QIODevice::Text);
        data_json=file.readAll();
        file.close();
    
    
        doc=doc.fromJson(data_json.toUtf8());
        obj=doc.object();
        QJsonArray jsonArray = obj["rows"].toArray();
    
        foreach (const QJsonValue & value, jsonArray) {
            QJsonObject obj = value.toObject();
            list_item.append(obj["id"].toString());
            if(id_op==obj["id"].toString())
            {
                list_item = obj.keys();
                QString dir = "dirección";
                ui->label_A1->setText(obj["nombre"].toString());
                ui->label_A2->setText(obj["fechadenacimiento"].toString());
                ui->label_A3->setText(obj["horadenacimiento"].toString());
                ui->label_A4->setText(obj["edad"].toString());
                ui->label_A5->setText(obj["estadocivil"].toString());
                ui->label_A6->setText(obj["pesoenkg"].toString());
                ui->label_A7->setText(obj["presiónarterial"].toString());
                ui->label_A8->setText(obj[dir.toUtf8()].toString());
                ui->label_A9->setText(obj["whatsapp"].toString());
                ui->label_A10->setText(obj["ocupación"].toString());
                ui->label_A11->setText(obj["sexo"].toString());
                ui->label_A12->setText(obj["lugardenacimiento"].toString());
                ui->label_A13->setText(obj["númerodehijoscantidad???"].toString());
                ui->label_A14->setText(obj["estaturaencm"].toString());
                ui->label_A15->setText(obj["correoelectrónico"].toString());
    
               //QStringList list_item = obj.keys();
    //           for(int i=0;i<list_item.size();i++)
    //           {
    //               list.append(obj[list[i]].toString());
    //           }
                list_item.append(id_op);
                ui->listWidget->addItems(list_item);
    

  • Lifetime Qt Champion

    Make sure you compile your source code with utf-8 and/or mark the strings explicitly as utf-8 string -> https://en.cppreference.com/w/cpp/language/string_literal



  • Thanks christian but i think i found my way around like so :

    QString dir = QString::fromLatin1("dirección");


  • Lifetime Qt Champion

    @hjiul This will not work when the locale is not the one you used during compilation (e.g. it will not work on linux). It simply works by accident.


Log in to reply