Simple JSON object
Construct a simple JSON object:
{
"Age": 27,
"Expert": true,
"Name": "Qter"
}
Generated as follows:
// 构建 JSON 对象
QJsonObject json;
json.insert("Name", "Qter");
json.insert("Age", 27);
json.insert("Expert", true);
// 构建 JSON 文档
QJsonDocument document;
document.setObject(json);
QByteArray byteArray = document.toJson(QJsonDocument::Compact);
QString strJson(byteArray);
qDebug() << strJson << endl;
The analysis is as follows:
QJsonParseError jsonError;
QJsonDocument doucment = QJsonDocument::fromJson(byteArray, &jsonError); // 转化为 JSON 文档
if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) // 解析未发生错误
{
if (doucment.isObject()) // JSON 文档为对象
{
QJsonObject object = doucment.object(); // 转化为对象
if (object.contains("Name")) // 包含指定的 key
{
QJsonValue value = object.value("Name"); // 获取指定 key 对应的 value
if (value.isString()) // 判断 value 是否为字符串
{
QString strName = value.toString(); // 将 value 转化为字符串
qDebug() << "Name : " << strName;
}
}
if (object.contains("Age"))
{
QJsonValue value = object.value("Age");
if (value.isDouble())
{
int nAge = value.toVariant().toInt();
qDebug() << "Age : " << nAge;
}
}
if (object.contains("Expert"))
{
QJsonValue value = object.value("Expert");
if (value.isBool())
{
bool bCrossPlatform = value.toBool();
qDebug() << "Expert : " << bCrossPlatform;
}
}
}
}
Simple JSON array
Construct a simple JSON object:
["Qt", 6, true]
Generated as follows:
// 构建 JSON 数组
QJsonArray json;
json.append("Qt");
json.append(6);
json.append(true);
// 构建 JSON 文档
QJsonDocument document;
document.setArray(json);
QByteArray byteArray = document.toJson(QJsonDocument::Compact);
QString strJson(byteArray);
qDebug() << strJson << endl;
The analysis is as follows:
QJsonParseError jsonError;
QJsonDocument doucment = QJsonDocument::fromJson(byteArray, &jsonError); // 转化为 JSON 文档
if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) // 解析未发生错误
{
if (doucment.isArray()) // JSON 文档为数组
{
QJsonArray array = doucment.array(); // 转化为数组
int nSize = array.size(); // 获取数组大小
for (int i = 0; i < nSize; ++i)// 遍历数组
{
QJsonValue value = array.at(i);
if (value.type() == QJsonValue::String)
{
QString strName = value.toString();
qDebug() << strName;
}
if (value.type() == QJsonValue::Double)
{
double dVersion = value.toDouble();
qDebug() << dVersion;
}
if (value.type() == QJsonValue::Bool)
{
bool bCrossPlatform = value.toBool();
qDebug() << bCrossPlatform;
}
}
}
}
Complex JSON object
Construct a complex JSON object:
{
"Name": "Qter",
"Age": 27,
"Sex": "Men",
"Skill": {
"One": "analysis",
"Two": "design",
"Three": "code"
},
"Salary": [
14,
20,
30,
50,
100
]
}
Generated as follows:
// 构建 Json 数组 - Salary
QJsonArray salaryArray;
salaryArray.append(14);
salaryArray.append(20);
salaryArray.append(30);
salaryArray.append(50);
salaryArray.append(100);
// 构建 Json 对象 - Skill
QJsonObject skillObject;
skillObject.insert("One", "analysis");
skillObject.insert("Two", "design");
skillObject.insert("Three", "code");
// 构建 Json 对象
QJsonObject json;
json.insert("Name", "Qter");
json.insert("Age", 27);
json.insert("Sex", "Men");
json.insert("Salary", QJsonValue(salaryArray));
json.insert("Page", QJsonValue(skillObject));
// 构建 Json 文档
QJsonDocument document;
document.setObject(json);
QByteArray byteArray = document.toJson(QJsonDocument::Compact);
QString strJson(byteArray);
qDebug() << strJson << endl;
The analysis is as follows:
QJsonParseError jsonError;
QJsonDocument doucment = QJsonDocument::fromJson(byteArray, &jsonError); // 转化为 JSON 文档
if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) // 解析未发生错误
{
if (doucment.isObject()) // JSON 文档为对象
{
QJsonObject object = doucment.object(); // 转化为对象
if (object.contains("Name"))
{
QJsonValue value = object.value("Name");
if (value.isString())
{
QString strName = value.toString();
qDebug() << "Name : " << strName;
}
}
if (object.contains("Age")) {
QJsonValue value = object.value("Age");
if (value.isString())
{
int nAge = value.toInt();
qDebug() << "Age : " << nAge;
}
}
if (object.contains("Sex")) {
QJsonValue value = object.value("Sex");
if (value.isDouble()) {
QString strSex = value.toVariant().toString();
qDebug() << "Sex : " << strSex;
}
}
if (object.contains("Salary"))
{
QJsonValue value = object.value("Salary");
if (value.isArray()) // Salary 的 value 是数组
{
QJsonArray array = value.toArray();
int nSize = array.size();
for (int i = 0; i < nSize; ++i)
{
QJsonValue value = array.at(i);
if (value.isDouble())
{
double dVersion = value.toDouble();
qDebug() << "Salary : " << dVersion;
}
}
}
}
if (object.contains("Skill"))
{
QJsonValue value = object.value("Skill");
if (value.isObject())
{
// Skill 的 value 是对象
QJsonObject obj = value.toObject();
if (obj.contains("One"))
{
QJsonValue value = obj.value("One");
if (value.isString())
{
QString strOne = value.toString();
qDebug() << "One : " << strOne;
}
}
if (obj.contains("Two"))
{
QJsonValue value = obj.value("Two");
if (value.isString())
{
QString strTwo = value.toString();
qDebug() << "Two : " << strTwo;
}
}
if (obj.contains("Three"))
{
QJsonValue value = obj.value("Three");
if (value.isString())
{
QString strThree = value.toString();
qDebug() << "Three : " << strThree;
}
}
}
}
}
}