{
"name":"value",
"secondName":{"thirdName":"thirdValue"},
"recoveryPrimaryNode":[
{"id":"1","iconTxt":"图片","iconName":"1.jpg"},
{"id":"2","iconTxt":"视频","iconName":"2.jpg"},
{"id":"3","iconTxt":"文本","iconName":"3.jpg"},
{"id":"4","iconTxt":"压缩包","iconName":"4.jpg"}]
}
#include "widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
createView();
}
void Widget::createView()
{
/*解析json文件*/
QFile file(":/data.json");
file.open(QIODevice::ReadOnly | QIODevice::Text);
QString value = file.readAll();
file.close();
QJsonParseError parseJsonErr;
QJsonDocument document = QJsonDocument::fromJson(value.toUtf8(),&parseJsonErr);
if(!(parseJsonErr.error == QJsonParseError::NoError))
{
qDebug()<<tr("解析json文件错误!");
return;
}
QJsonObject jsonObject = document.object();
qDebug()<<"jsonObject[name]=="<<jsonObject["name"].toString();
if(jsonObject.contains(QStringLiteral("secondName")))
{
QJsonValue jsonValueList = jsonObject.value(QStringLiteral("secondName"));
QJsonObject item = jsonValueList.toObject();
qDebug()<<"item[thirdName]=="<<item["thirdName"].toString();
}
if(jsonObject.contains(QStringLiteral("recoveryPrimaryNode")))
{
QJsonValue arrayValue = jsonObject.value(QStringLiteral("recoveryPrimaryNode"));
if(arrayValue.isArray())
{
QJsonArray array = arrayValue.toArray();
for(int i=0;i<array.size();i++)
{
QJsonValue iconArray = array.at(i);
QJsonObject icon = iconArray.toObject();
QString id = icon["id"].toString();
QString iconTxt = icon["iconTxt"].toString();
QString iconName = icon["iconName"].toString();
qDebug()<<"id="<<id<<"iconTxt="<<iconTxt<<"iconName="<<iconName;
}
}
}
}