-1

i have a json file that looks like this

{
  "userList": [
      {
        "id": "1",
        "Name": "Tom",
        "lastName": "Cruise",
        "email": "tom@gmail.com",
        "gender": "male",
        "dateOfBirth": "9.9.1990",
        "street": "tom Cruse street",
        "city": "New york",
        "postalCode": "11445",
        "phone": "123123123",
        "imagePath": "0"
      },
   ]
}

now, i need on pushButton_clicked to read all data and write it in labels. I just started Qt, and its my first time using it, and Internet is not full of information. Can you help me?

QFile file("C:/Users/CZV/Documents/userList.json");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
   qDebug() << "Failed to open file";
}
QJsonDocument doc=QJsonDocument::fromJson(file.readAll());

QJsonObject obj = doc.object(); // get root
QJsonValue idVal =obj.value("id"); // get value for key "name"
QJsonValue nameVal =obj.value("Name");
QJsonValue lastNameVal =obj.value("lastName");
QJsonValue emailVal =obj.value("email");
QJsonValue genderVal =obj.value("gender");
QJsonValue dobVal =obj.value("dateOfBirth");
QJsonValue streetVal =obj.value("street");
QJsonValue cityVal =obj.value("city");
QJsonValue postVal =obj.value("postalCode");
QJsonValue phoneVal =obj.value("phone");
QJsonValue imgVal =obj.value("imagePath");
ui->clabelName->setText(nameVal.toString());
ui->clabelLastName->setText(nameVal.toString());
ui->clabelEmail->setText(emailVal.toString());
ui->clabelGender->setText(genderVal.toString());
ui->clabelDoB->setText(dobVal.toString());
ui->clabelStreet->setText(streetVal.toString());
ui->clabelCity->setText(cityVal.toString());
ui->clabelPost->setText(postVal.toString());
ui->clabelPhone->setText(phoneVal.toString());`

this is a code i try to use, but it doesnt work

kiner_shah
  • 3,939
  • 7
  • 23
  • 37
  • 3
    Your code assumes there is only a single root object, while you have an array nested under the key `userList`. You only seem to have a single set of labels, so do you want to pick the first user in the list? the last? – Botje Mar 20 '23 at 10:09
  • 1
    "[I]t doesnt work" ***how?*** Please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Then [edit] your question to improve it. – Some programmer dude Mar 20 '23 at 10:10
  • 4
    *The internet is not full of information*, Have you seen [this](https://doc.qt.io/qt-6/json.html)? As stated above the clear problem with your code is that the data contains a field called "userList" but where does that string appear in your code (apart from the file name)? – john Mar 20 '23 at 10:15
  • @john i have seen that, but i dont understand it, i didnt just rush here to ask question, i did my research and couldnt find anything to help me. I ve been on this problem for past 3 days. My problem is that i dont know whats missing in code, i dont know what i am doing wrong. i put my code here for ppl to see my logic(if there is one), but what i am asking for is a new code that will solve my problem. If u can help me, great, thank you, i would buy you a beer, but if u cant, thanks for commenting anyway. – Marko Mitrović Mar 20 '23 at 10:38
  • @Botje i want to my clabelName change text to Tom, clabelLastName to Cruise, etc... I understand what u r trying to say about "single root object", but i dont know how to solve that problem. – Marko Mitrović Mar 20 '23 at 10:43
  • @MarkoMitrović Well you got an answer below. This issue is that your code ignores the `"userList"` that is in your data (as well as the array `[]`). The fact that your data has `userList` but your code does not is a clue that something is not right. – john Mar 20 '23 at 10:50

1 Answers1

2

Your JSON has root object with one key "userList" whose value is an array. That array's 0th index object is what you want.

QJsonObject root_obj = doc.object();
QJsonArray user_list = root_obj.value("userList").toArray();
QJsonObject user_obj = user_list[0].toObject();
QJsonValue nameVal = user_obj.value("Name");
QJsonValue lastNameVal = user_obj.value("lastName");
...

NOTE: Your JSON has a minor error, it has an extra , at the end of the array which may cause issues in parsing the document since it's not supported by the JSON specification.

kiner_shah
  • 3,939
  • 7
  • 23
  • 37