QFile loadFile("profile.json");
if (!loadFile.open(QIODevice::ReadOnly))
{
qDebug() << "could't open projects json";
return false;
}
QByteArray allData = loadFile.readAll();
loadFile.close();
QJsonParseError json_error;
QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));
if (json_error.error != QJsonParseError::NoError)
{
qDebug() << "json error!";
return false;
}
QJsonObject rootObj = jsonDoc.object();
QStringList keys = rootObj.keys();
for (int i = 0; i < keys.size(); i++)
{
qDebug() << "key" << i << " is:" << keys.at(i);
}
//因为是预先定义好的JSON数据格式,所以这里可以这样读取
if (rootObj.contains("profile") && rootObj["profile"].isObject())
{
QJsonObject subObj = rootObj["profile"].toObject();
if (subObj.contains(strAxis) && subObj[strAxis].isObject())
{
QJsonObject subObjAxis = subObj[strAxis].toObject();
if (subObjAxis.contains(strKey) && subObjAxis[strKey].isBool())
{
return subObjAxis[strKey].toBool();
}
}
}