void CommentDataManger::readJsonFile()
{
QJsonObject json;
QString strPath = QCoreApplication::applicationDirPath();
QDir dir(strPath);
dir.cd("config/");
QString _strPath = dir.absolutePath() + "/myFile.json";
QFile _file(_strPath);
if (_file.open(QIODevice::ReadWrite))
{
QByteArray _content = _file.readAll();
QJsonParseError error;
QJsonDocument _doc = QJsonDocument::fromJson(_content, &error);
if (!_doc.isEmpty())
{
json = _doc.object();
//...数据解析
}
_file.close();
}
}
本文介绍了一种使用C++和Qt库读取JSON配置文件的方法。通过QFile、QJsonDocument和QJsonObject等类,实现了从指定路径读取并解析JSON文件的功能。此过程包括打开文件、读取文件内容、解析JSON数据以及关闭文件。
2648

被折叠的 条评论
为什么被折叠?



