Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. struggle with json object keys name and utf8

struggle with json object keys name and utf8

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hjiul
    wrote on last edited by hjiul
    #1

    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);
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • H Offline
        H Offline
        hjiul
        wrote on last edited by
        #3

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

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

        Christian EhrlicherC 1 Reply Last reply
        0
        • H hjiul

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

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

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved