Evgenii Legotckoi
Oct. 27, 2018, 3:35 a.m.

Qt/C++ - Tutorial 085. Work with QJsonObject, QJsonArray, QJsonDocument. Save and load JSON from files

Consider a small example of the formation of a JSON document from, for example, the text and the title of this text.

For example, the text has:

  • Title - First Title
  • Content - First Content

And so on

We will add this text to QJsonObject, which we will add to the QJsonArray text array. The array of objects will be in the general working QJsonObject, which we will save to a file.

We will do all this through a graphical interface in which we have:

  • QLineEdit - titleLineEdit - contains the title of the text to add
  • QTextEdit - contentTextEdit - contains the contents of the text to add
  • QTextEdit - jsonDocumentTextEdit - JSON document preview
  • QPushButton - addButton - button to add new text in JSON
  • QPushButton - clearButton - button to remove all texts from the current QJsonObject
  • QPushButton - saveButton - button to save JSON document to file
  • QPushButton - loadButton - button to read JSON from file

In this case, we will be able to read the JSON file and add additional texts to the read file.

The application will look like this.


Implementation

widget.h

  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3.  
  4. #include <QWidget>
  5. #include <QJsonObject>
  6.  
  7. namespace Ui {
  8. class Widget;
  9. }
  10.  
  11. class Widget : public QWidget
  12. {
  13. Q_OBJECT
  14.  
  15. public:
  16. explicit Widget(QWidget *parent = nullptr);
  17. ~Widget();
  18.  
  19. private:
  20. // Slot of adding new text to the array of texts
  21. void onAddButtonClicked();
  22. // Слот удаления всей информации о текстах в текущем объекте
  23. void onClearButtonClicked();
  24. // Slot save text in json file
  25. void onSaveButtonClicked();
  26. // Slot of loading texts into the program from json file
  27. void onLoadButtonClicked();
  28.  
  29. Ui::Widget *ui;
  30.  
  31. // The current json object you are working with
  32. QJsonObject m_currentJsonObject;
  33. };
  34.  
  35. #endif // WIDGET_H

widget.cpp

  1. #include "widget.h"
  2. #include "ui_widget.h"
  3.  
  4. #include <QFileDialog>
  5. #include <QFile>
  6. #include <QDir>
  7. #include <QJsonObject>
  8. #include <QJsonArray>
  9. #include <QJsonDocument>
  10.  
  11. Widget::Widget(QWidget *parent) :
  12. QWidget(parent),
  13. ui(new Ui::Widget)
  14. {
  15. ui->setupUi(this);
  16.  
  17. // Connecting handler slots to button click signals
  18. connect(ui->addButton, &QPushButton::clicked, this, &Widget::onAddButtonClicked);
  19. connect(ui->clearButton, &QPushButton::clicked, this, &Widget::onClearButtonClicked);
  20. connect(ui->saveButton, &QPushButton::clicked, this, &Widget::onSaveButtonClicked);
  21. connect(ui->loadButton, &QPushButton::clicked, this, &Widget::onLoadButtonClicked);
  22. }
  23.  
  24. Widget::~Widget()
  25. {
  26. delete ui;
  27. }
  28.  
  29. void Widget::onAddButtonClicked()
  30. {
  31. // Create a text object
  32. QJsonObject textObject;
  33. textObject["title"] = ui->titleLineEdit->text(); // Set the text header
  34. textObject["content"] = ui->contentTextEdit->toPlainText(); // Set the content of the text
  35. QJsonArray textsArray = m_currentJsonObject["texts"].toArray(); // We take the current array of texts, even if it does not exist, it will be created automatically
  36. textsArray.append(textObject); // Add a text object to the array
  37. m_currentJsonObject["texts"] = textsArray; // Save the array back to the current object.
  38.  
  39. // Set the text of the entire Json object in the text field for verification
  40. ui->jsonDocumentTextEdit->setText(QJsonDocument(m_currentJsonObject).toJson(QJsonDocument::Indented));
  41. }
  42.  
  43. void Widget::onClearButtonClicked()
  44. {
  45. m_currentJsonObject = QJsonObject(); // Re-create the new current QJsonObject
  46. ui->jsonDocumentTextEdit->clear(); // Clear the text field
  47. // Set the text of the entire Json object in the text field to see that it is an empty object.
  48. // See the following -> {}
  49. ui->jsonDocumentTextEdit->setText(QJsonDocument(m_currentJsonObject).toJson(QJsonDocument::Indented));
  50. }
  51.  
  52. void Widget::onSaveButtonClicked()
  53. {
  54. // Using the dialog box we get the file name with the absolute path.
  55. QString saveFileName = QFileDialog::getSaveFileName(this,
  56. tr("Save Json File"),
  57. QString(),
  58. tr("JSON (*.json)"));
  59. QFileInfo fileInfo(saveFileName); // Using QFileInfo
  60. QDir::setCurrent(fileInfo.path()); // set the current working directory where the file will be, otherwise it may not work
  61. // Create a file object and open it for writing.
  62. QFile jsonFile(saveFileName);
  63. if (!jsonFile.open(QIODevice::WriteOnly))
  64. {
  65. return;
  66. }
  67.  
  68. // Write the current Json object to a file.
  69. jsonFile.write(QJsonDocument(m_currentJsonObject).toJson(QJsonDocument::Indented));
  70. jsonFile.close(); // Close file
  71. }
  72.  
  73. void Widget::onLoadButtonClicked()
  74. {
  75. // Choose a file
  76. QString openFileName = QFileDialog::getOpenFileName(this,
  77. tr("Open Json File"),
  78. QString(),
  79. tr("JSON (*.json)"));
  80. QFileInfo fileInfo(openFileName); // Using QFileInfo
  81. QDir::setCurrent(fileInfo.path()); // set the current working directory where the file will be
  82. // Create a file object and open it for reading.
  83. QFile jsonFile(openFileName);
  84. if (!jsonFile.open(QIODevice::ReadOnly))
  85. {
  86. return;
  87. }
  88.  
  89. // Read the entire file
  90. QByteArray saveData = jsonFile.readAll();
  91. // Create QJsonDocument
  92. QJsonDocument jsonDocument(QJsonDocument::fromJson(saveData));
  93. // From which we select an object into the current working QJsonObject
  94. m_currentJsonObject = jsonDocument.object();
  95. // Clear the text field
  96. ui->jsonDocumentTextEdit->clear();
  97. // And set the contents of the Json object in the verification text field.
  98. ui->jsonDocumentTextEdit->setText(QJsonDocument(m_currentJsonObject).toJson(QJsonDocument::Indented));
  99. }

Conclusion

In this particular case, QFileDialog is used to get the path to the recordable file and the file being opened. Of course, from manipulating an existing file, only adding new texts is available and you can open any correct JSON document and then add text to it. But for example, I believe that more is not needed.

Example on GitHub repository of EVILEG

Recommended articles on this topic

By article asked0question(s)

5

Do you like it? Share on social networks!

Михаиллл
  • Oct. 28, 2018, 9:41 p.m.

Скажите пожалуйста, в 55 строке получаем saveFileNam, это видимо полное имя файла. Но где потом сообщается полный путь  файлу json при его записи?

Evgenii Legotckoi
  • Oct. 28, 2018, 9:50 p.m.

В saveFileName будет полный путь к файлу с его именем. В диалоге просто вводите имя файла с расширением, а QFileDialog возвращает имя файла и полный путь к нему.

Можете увидеть это, если добавите вывод в qDebug

  1. qDebug() << saveFileName;

А через fileInfo я выдернул путь к файлу, чтобы установить рабочую директорию через QDir::setCurrent();, чтобы указать, где именно сохраняется файл. А когда я передаю saveFileName в QFile, даже с наличием пути в имени, всё будет нормально работать, путь должен быть отброшен автоматически.


Михаиллл
  • Oct. 29, 2018, 12:12 p.m.

Спасибо.

Скажите пожалуйста, зачем 65 строка? написал по образцу  часть кода, выдало ошибку D:\QTProject\ReaderResume\main.cpp:58: ошибка: return-statement with no value, in function returning 'int' [-fpermissive]

return;

^

Без return работает


Evgenii Legotckoi
  • Oct. 29, 2018, 2:08 p.m.

зачем вы слот написали с возвращаемым значением int?

У вас сигнатура в вашем коде так написана

  1. int someFunc();

слоту обычно не требуется возвращаемое значение.

return прерывает выполнение метода, чтобы не шло дальше, если файл не открылся в данном случае.


Михаиллл
  • Oct. 29, 2018, 2:26 p.m.

Спасибо

Скажите пожалуйста, как добавить в массив QJsonArray другой массив QVector<QString> TextArra?

Как я понял, это можно сделать в цикле поочередно для каждого члена массива, но можно ли сразу перевести одной командой?



Evgenii Legotckoi
  • Oct. 29, 2018, 2:40 p.m.

Думаю, что через алгоритмические методы вставки из стандартной библиотеки и использование итераторов с лямбда функциями можно написать и в одну строку.

Но судя по вашим вопросам, могу сказать, что вы не знакомы со всем этим зоопарком, поэтому пока не забивайте себе этим голову, в процессе работы сами прийдёте к использованию данного функционала. Так что сделайте через цикл поочерёдно. Просто напишите

  1. QJsonArray textsArray;
  2. for (const QString& str : TextArray)
  3. {
  4. textsArray.push_back(str);
  5. }



Также вы можете сами написать эту вспомогательную команду.

Михаиллл
  • Oct. 29, 2018, 9:07 p.m.

Спасибо. И скажите пожалуйста, как перевести QString в QJsonValue


Evgenii Legotckoi
  • Oct. 29, 2018, 9:18 p.m.

QJsonValue::fromVariant(const QVariant& variant);

Михаиллл
  • Oct. 29, 2018, 10:02 p.m.

Спасибо.

Скажите пожалуйста, в json файле размещен массив, в этот массив нужно добавить еще один элемент массива. Но считывать весь массив - тратить дополнительное время. Можно ли, при условии, что я знаю, что там только один массив, как то быстро добавить один элемент?


Evgenii Legotckoi
  • Oct. 30, 2018, 1:22 a.m.

Возможно, через итератор можно будет сделать. Я не проверял. сам по себе метод toArray() возвращает копию массива...

На первый взгляд я не увидел необходимой возможности, хотя подумал об этом.

Возможно с итераторами через метод find, можно будет взять массив и уже в нём покопаться. Но не факт.


Михаиллл
  • Oct. 31, 2018, 2:50 p.m.

А можно сразу перейти в конец документа, если нет ], то записывать с начала, а если есть ] , то ставить запятую и дописывать еще один элемент?

Evgenii Legotckoi
  • Oct. 31, 2018, 2:52 p.m.

Это уже вручную нужно реализовывать. Только это не нужно, пока действительно не будет проблемы по производительности.

А вообще, если нужно хранение данных, то возможно и насчёт базы данных стоит подумать. Хотя бы SQLite можно использовать. Это будет эффективнее.


Юрий
  • July 1, 2020, 3:34 a.m.

Как отредактировать данные?

  1. {
  2. "connect": [
  3. {
  4. "id": 1,
  5. "name": "Data1",
  6. },
  7. {
  8. "id": 2,
  9. "name": "Data2",
  10. }
  11. ]
  12. }
  13.  
Михаиллл
  • July 1, 2020, 12:54 p.m.

Смотря что хотите и в каком виде эти данные. Если в текстовом, то их нужно прочитать, перевести в QJsonDocument, из него достать массив connect, и уже обращаясь у элементам массива, получвя из него объекты редактировать их

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • AK
    April 1, 2025, 11:41 a.m.
    Добрый день. В данный момент работаю над проектом, где необходимо выводить звук из программы в определенное аудиоустройство (колонки, наушники, виртуальный кабель и т.д). Пишу на Qt5.12.12 поско…
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup