Json文件是这样:
{
"first fruit":
{
"describe":"an apple",
"icon":"appleIcon",
"name":"apple"
},
"second fruit":
{
"describe":"an orange",
"icon":"orangeIcon",
"name":"orange"
},
"three fruit array":
[
"eat 0",
"eat 1",
"eat 2",
"eat 3",
"eat 4"
]
}
存放在本地磁盘。
利用QJson可以方便的进行解析。
QFile loadFile("D:\\1.json");
if(!loadFile.open(QIODevice::ReadOnly))
{
qDebug() << "could't open projects json";
return;
}
QByteArray allData = loadFile.readAll();
loadFile.close();
QJsonParseError jsonError;
QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &jsonError));
if(jsonError.error != QJsonParseError::NoError)
{
qDebug() << "json error!" << jsonError.errorString();
return;
}
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("first fruit"))
{
QJsonObject subObj = rootObj.value("first fruit").toObject();
qDebug() << "describe is:" << subObj["describe"].toString();
qDebug() << "icon is:" << subObj["icon"].toString();
qDebug() << "name is:" << subObj["name"].toString();
}
if(rootObj.contains("second fruit"))
{
QJsonObject subObj = rootObj.value("second fruit").toObject();
qDebug() << "describe is:" << subObj.value("describe").toString();
qDebug() << "icon is:" << subObj.value("icon").toString();
qDebug() << "name is:" << subObj.value("name").toString();
}
if(rootObj.contains("three fruit array"))
{
QJsonArray subArray = rootObj.value("three fruit array").toArray();
for(int i = 0; i< subArray.size(); i++)
{
qDebug() << i<<" value is:" << subArray.at(i).toString();
}
}
需要包含的头文件:
#include <QJsonDocument>
#include <QJsonParseError>
#include <QFile>
#include <QJsonObject>
#include <QDebug>
#include <QJsonArray>
运行结果:
key 0 is: "first fruit"
key 1 is: "second fruit"
key 2 is: "three fruit array"
describe is: "an apple"
icon is: "appleIcon"
name is: "apple"
describe is: "an orange"
icon is: "orangeIcon"
name is: "orange"
0 value is: "eat 0"
1 value is: "eat 1"
2 value is: "eat 2"
3 value is: "eat 3"
4 value is: "eat 4"
Qt官方文档JSON Save Game Example
也可以看:JSON实战之savegame(Qt Example)
每次读取之前,都要判断
void Character::read(const QJsonObject &json)
{
if (json.contains("name") && json["name"].isString())
mName = json["name"].toString();
if (json.contains("level") && json["level"].isDouble())
mLevel = json["level"].toInt();
if (json.contains("classType") && json["classType"].isDouble())
mClassType = ClassType(json["classType"].toInt());
}
先用contains判断是否存在,再用isString判断是能转换。
bool isArray() const
bool isBool() const
bool isDouble() const
bool isNull() const
bool isObject() const
bool isString() const
bool isUndefined() const
修改后的代码为:
QFile loadFile("D:\\1.json");
if(!loadFile.open(QIODevice::ReadOnly))
{
qDebug() << "could't open projects json";
return;
}
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;
}
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("first fruit") && rootObj["first fruit"].isObject())
{
QJsonObject subObj = rootObj["first fruit"].toObject();
if (subObj.contains("describe") && subObj["describe"].isString())
{
qDebug() << "describe is:" << subObj["describe"].toString();
}
if (subObj.contains("icon") && subObj["icon"].isString())
{
qDebug() << "icon is:" << subObj["icon"].toString();
}
if (subObj.contains("name") && subObj["name"].isString())
{
qDebug() << "name is:" << subObj["name"].toString();
}
}
if(rootObj.contains("second fruit") && rootObj["second fruit"].isObject())
{
QJsonObject subObj = rootObj["second fruit"].toObject();
if (subObj.contains("describe") && subObj["describe"].isString())
{
qDebug() << "describe is:" << subObj["describe"].toString();
}
if (subObj.contains("icon") && subObj["icon"].isString())
{
qDebug() << "icon is:" << subObj["icon"].toString();
}
if (subObj.contains("name") && subObj["name"].isString())
{
qDebug() << "name is:" << subObj["name"].toString();
}
}
if(rootObj.contains("three fruit array") && rootObj["three fruit array"].isArray())
{
QJsonArray subArray = rootObj.value("three fruit array").toArray();
for(int i = 0; i< subArray.size(); i++)
{
if (subArray[i].isString())
{
qDebug() << i<<" value is:" << subArray.at(i).toString();
}
}
}