当前位置:网站首页>使用QT遍历Json文档及搜索子对象
使用QT遍历Json文档及搜索子对象
2022-07-05 17:33:00 【黑脚印_DarkSpoor】
关于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() ";
}
}
};
边栏推荐
- Please tell me why some tables can find data by writing SQL, but they can't be found in the data map, and the table structure can't be found
- Tita performance treasure: how to prepare for the mid year examination?
- Oracle Recovery Tools ----oracle数据库恢复利器
- CVPR 2022最佳学生论文:单张图像估计物体在3D空间中的位姿估计
- Cmake tutorial Step3 (requirements for adding libraries)
- Cartoon: interesting [pirate] question
- 求解为啥all(())是True, 而any(())是FALSE?
- Knowledge points of MySQL (7)
- Thesis reading_ Chinese NLP_ LTP
- Beijing internal promotion | the machine learning group of Microsoft Research Asia recruits full-time researchers in nlp/ speech synthesis and other directions
猜你喜欢
随机推荐
Server configuration jupyter environment
如何保存训练好的神经网络模型(pytorch版本)
查看自己电脑连接过的WiFi密码
力扣解法汇总729-我的日程安排表 I
Oracle recovery tools -- Oracle database recovery tool
ITK Example
Matlab reference
漫画:如何实现大整数相乘?(下)
漫画:如何实现大整数相乘?(上) 修订版
漫画:如何实现大整数相乘?(整合版)
Which platform of outer disk gold is regular and safe, and how to distinguish it?
CVPR 2022最佳学生论文:单张图像估计物体在3D空间中的位姿估计
「运维有小邓」用于云应用程序的单点登录解决方案
漫画:寻找无序数组的第k大元素(修订版)
Please tell me why some tables can find data by writing SQL, but they can't be found in the data map, and the table structure can't be found
北京内推 | 微软亚洲研究院机器学习组招聘NLP/语音合成等方向全职研究员
Cartoon: how to multiply large integers? (integrated version)
Cmake tutorial step6 (add custom commands and generate files)
EPM related
flask接口响应中的中文乱码(unicode)处理