使用 QT 逐行读取 JSON
read JSON line by line with QT
我有一个这样的JSON:
{
"09:32 24.09.2018": "456",
"09:56 24.09.2018": "854",
"09:57 24.09.2018": "854",
"09:59 24.09.2018": "853",
"10:00 24.09.2018": "369",
"10:01 24.09.2018": "369"
}
如何在 QT5 中逐行阅读此内容。我想把它写成QTableWidget
,在第一行/第一列有"09:32 24.09.2018",在第一行/第二列有"456",在第一行/第二列有"09:56 24.09.2018"第二行/第一列等等。
我当前的加载工作方式如下:
QJsonDocument getFile = loadJson("test");
QJsonObject obj = getFile.object();
跟:
QJsonDocument MainWindow::loadJson(QString fileName) {
QFile jsonFile(fileName);
jsonFile.open(QFile::ReadOnly);
return QJsonDocument().fromJson(jsonFile.readAll());
}
谢谢。
你可以遍历QJsonObject来横向它:
QString val;
QFile file;
//set the file name
file.setFileName("foo.json");
//open the file name
file.open(QIODevice::ReadOnly | QIODevice::Text);
//read the file name
val = file.readAll();
//close the file name
file.close();
//string to json doc
QJsonDocument doc = QJsonDocument::fromJson(val.toUtf8());
//json doc to json object
QJsonObject object = doc.object();
//json object can be iterated
for (auto it = object.begin(), end=object.end(); it != end; ++it)
{
qDebug() << "Key: " << it.key() << "Val: " << it.value();
}
相关文章:
- 使用 minijson-reader 库读取 JSON 字符串时出现问题
- 如何在Visual C+++中从JSON文件中读取节点数据
- 使用 QT 逐行读取 JSON
- 如何在 C++ 中使用提升属性树从 JSON 文件中读取对象数组
- 从Qt QJsonDocument::toBinaryData使用non-Qt读取Binary Json?
- 从 JSON 读取和保存数据
- 使用 jsoncpp 读取 JSON 文件
- 如何使用 rapidjson 读取 json 文件并输出到 std::string?
- 如何按C++读取 JSON 内容?
- ESP8266读取 JSON,但不读取 PHP 文件
- 如何将值数组从 json 文件读取到 c++ 数组
- 使用 boost::asio 读取 JSON 流,获取完整字符串
- 使用Boost Property_tree读取JSON
- c++ jsoncpp 读取 json 文件
- 使用 boost 读取复杂的 JSON 数组
- 在 c++ 中读取配置文件 json 并没有停止
- C nonlohmann json读取子对象
- C Nlohmann/JSON如何使用运行时提供JSON_POINTERS读取JSON值
- 使用Boost Json读取JSON消息
- Boost属性树Json读取包含LPWSTR的文件