关于qt遍历Json文档的代码,整理并记录在这里,方便以后使用。
QT 的版本如下图
Json文件内容:
{
"动物": {
"人": {
"年龄": 43,
"电话": [
"+123 12345678",
"+321 87654321"
]
},
"狗": {
"腿": [
"左前腿",
"右前腿",
"左后腿",
"右后腿"
],
"头": {
"眼睛": 2,
"鼻子": 1,
"耳朵": 2
}
}
}
}
使用QJsonDocument遍历所有对象的输出如下:
"" "根" isObject() 有 1 个子对象
" " "动物" isObject() 有 2 个子对象
" " "人" isObject() 有 2 个子对象
" " "年龄" isDouble() 43
" " "电话" isArray() 有 2 个子对象
" " "" isString() "+123 12345678"
" " "" isString() "+321 87654321"
" " "狗" isObject() 有 2 个子对象
" " "头" isObject() 有 3 个子对象
" " "眼睛" isDouble() 2
" " "耳朵" isDouble() 2
" " "鼻子" isDouble() 1
" " "腿" isArray() 有 4 个子对象
" " "" isString() "左前腿"
" " "" isString() "右前腿"
" " "" isString() "左后腿"
" " "" isString() "右后腿"
遍历并输出Json对象的代码:
//加载Json文件
static void LoadJsonFromFile(const QString& jsonfilename,QJsonDocument& jsDoc)
{
QFile file(jsonfilename);
file.open(QIODevice::ReadWrite);
QByteArray json = file.readAll();
jsDoc = QJsonDocument::fromJson(json);
}
//遍历输出Json对象
//输入是Json文档
static void PrintJson(QJsonDocument& jDoc)
{
if(jDoc.isObject())
{
QJsonObject jObject = jDoc.object(); //获得根对象
PrintJsonObject(jObject,"根");
}
else if(jDoc.isArray())
{
QJsonArray jArray = jDoc.array();
PrintJsonArray(jArray,"");
}
else
{
//空文档
qDebug() << "空文档";
}
}
//遍历QJsonObject
static void PrintJsonObject(QJsonObject& obj,const QString& key,int retract=0)
{
//输出使用的缩进
QString tabs = "";
for(int i=0;i<retract;++i) {tabs+=" ";}
qDebug() << tabs << key << "isObject() 有 " << obj.size() << " 个子对象 ";
for(const QString& key: obj.keys()) //遍历所有的key
{
QJsonValue jvalue = obj[key];
PrintJsonValue(jvalue,key,retract+1); //打印输出QJsonValue
}
}
//遍历QJsonArray
static void PrintJsonArray(QJsonArray& arr,const QString& key,int retract=0)
{
//输出使用的缩进
QString tabs = "";
for(int i=0;i<retract;++i) {tabs+=" ";}
qDebug() << tabs << key << "isArray() 有 " << arr.size() << " 个子对象 ";
for(QJsonArray::iterator it=arr.begin();it!=arr.end();++it) //遍历数组内的对象
{
QJsonValue jvalue = *it;
PrintJsonValue(jvalue,"",retract+1); //打印输出QJsonValue
}
}
//打印输出QJsonValue
static void PrintJsonValue(QJsonValue& jvalue,const QString& key,int retract=0)
{
//输出使用的缩进
QString tabs = "";
for(int i=0;i<retract;++i) {tabs+=" ";}
if(jvalue.isArray())
{
QJsonArray childArray = jvalue.toArray();
PrintJsonArray(childArray,key,retract); //递归调用
}
else if(jvalue.isObject())
{
QJsonObject childObject = jvalue.toObject();
PrintJsonObject(childObject,key,retract); //递归调用
}
else if(jvalue.isBool())
{
qDebug() << tabs << key << " isBool() " << jvalue.toBool();
}
else if(jvalue.isDouble())
{
qDebug() << tabs << key << " isDouble() " << jvalue.toDouble();
}
else if(jvalue.isString())
{
qDebug() << tabs << key << " isString() " << jvalue.toString();
}
else if(jvalue.isUndefined())
{
qDebug() << tabs << " isUndefined() ";
}
else if(jvalue.isNull())
{
qDebug() << tabs << " isNull() ";
}
}
//测试代码
QJsonDocument doc;
ZJsonTools::LoadJsonFromFile(QString("json文件的完整路径名称"),doc);
ZJsonTools::PrintJson(doc);
搜索Json的代码:
static bool SearchJsonValue(const QJsonObject& jObject,const QStringList& path,QJsonValue& result)
{
QJsonValue jValue = jObject;
return SearchJsonValue(jValue,path,result);
}
static bool SearchJsonValue(const QJsonValue& jValue,const QStringList& path,QJsonValue& result)
{
result = jValue;
for(int i=0;i<path.length();++i)
{
result = result[path[i]];
if(result.isNull() || result.isUndefined())
{
return false;
}
}
return true;
}
//测试代码
QStringList path = {"动物","狗","头","鼻子"};
QJsonObject jObject = doc.object();
QJsonValue jObj;
if(ZJsonTools::SearchJsonValue(jObject,path,jObj))
{
qDebug()<< " 狗鼻子的数量: " << jObj.toInt();
}
else
{
qDebug()<< " 未找到 ";
}
搜索Json测试代码输出:
狗鼻子的数量: 1
完整的ZJsonTools类代码:
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QJsonArray>
#include <QString>
#include <QFile>
#include <QList>
#include <QDebug>
class ZJsonTools final
{
private:
ZJsonTools();
public:
static void LoadJsonFromFile(const QString& jsonfilename,QJsonDocument& jsDoc)
{
QFile file(jsonfilename);
file.open(QIODevice::ReadWrite);
QByteArray json = file.readAll();
jsDoc = QJsonDocument::fromJson(json);
}
static void PrintJson(QJsonDocument& jDoc)
{
if(jDoc.isObject())
{
QJsonObject jObject = jDoc.object();
PrintJsonObject(jObject,"根");
}
else if(jDoc.isArray())
{
QJsonArray jArray = jDoc.array();
PrintJsonArray(jArray,"");
}
else
{
//空文档
qDebug() << "空文档";
}
}
static bool SearchJsonValue(const QJsonObject& jObject,const QStringList& path,QJsonValue& result)
{
QJsonValue jValue = jObject;
return SearchJsonValue(jValue,path,result);
}
static bool SearchJsonValue(const QJsonValue& jValue,const QStringList& path,QJsonValue& result)
{
result = jValue;
for(int i=0;i<path.length();++i)
{
result = result[path[i]];
if(result.isNull() || result.isUndefined())
{
return false;
}
}
return true;
}
private:
static void PrintJsonObject(QJsonObject& obj,const QString& key,int retract=0)
{
//输出使用的缩进
QString tabs = "";
for(int i=0;i<retract;++i) {tabs+=" ";}
qDebug() << tabs << key << "isObject() 有 " << obj.size() << " 个子对象 ";
for(const QString& key: obj.keys())
{
QJsonValue jvalue = obj[key];
PrintJsonValue(jvalue,key,retract+1);
}
}
static void PrintJsonArray(QJsonArray& arr,const QString& key,int retract=0)
{
//输出使用的缩进
QString tabs = "";
for(int i=0;i<retract;++i) {tabs+=" ";}
qDebug() << tabs << key << "isArray() 有 " << arr.size() << " 个子对象 ";
for(QJsonArray::iterator it=arr.begin();it!=arr.end();++it)
{
QJsonValue jvalue = *it;
PrintJsonValue(jvalue,"",retract+1);
}
}
static void PrintJsonValue(QJsonValue& jvalue,const QString& key,int retract=0)
{
//输出使用的缩进
QString tabs = "";
for(int i=0;i<retract;++i) {tabs+=" ";}
if(jvalue.isArray())
{
QJsonArray childArray = jvalue.toArray();
PrintJsonArray(childArray,key,retract);
}
else if(jvalue.isObject())
{
QJsonObject childObject = jvalue.toObject();
PrintJsonObject(childObject,key,retract);
}
else if(jvalue.isBool())
{
qDebug() << tabs << key << " isBool() " << jvalue.toBool();
}
else if(jvalue.isDouble())
{
qDebug() << tabs << key << " isDouble() " << jvalue.toDouble();
}
else if(jvalue.isString())
{
qDebug() << tabs << key << " isString() " << jvalue.toString();
}
else if(jvalue.isUndefined())
{
qDebug() << tabs << " isUndefined() ";
}
else if(jvalue.isNull())
{
qDebug() << tabs << " isNull() ";
}
}
};