当前位置:网站首页>使用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() ";
}
}
};边栏推荐
- rsync
- Short the command line via jar manifest or via a classpath file and rerun
- Cmake tutorial Step4 (installation and testing)
- Flask solves the problem of CORS err
- ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
- 7 pratiques devops pour améliorer la performance des applications
- 統計php程序運行時間及設置PHP最長運行時間
- Which is more cost-effective, haqu K1 or haqu H1? Who is more worth starting with?
- Ant financial's sudden wealth has not yet begun, but the myth of zoom continues!
- This 17-year-old hacker genius cracked the first generation iPhone!
猜你喜欢

CVPR 2022 best student paper: single image estimation object pose estimation in 3D space

网络威胁分析师应该具备的十种能力

提高应用程序性能的7个DevOps实践

求解为啥all(())是True, 而any(())是FALSE?

leetcode每日一题:字符串中的第一个唯一字符

Configure pytorch environment in Anaconda - win10 system (small white packet meeting)

「运维有小邓」用于云应用程序的单点登录解决方案

统计php程序运行时间及设置PHP最长运行时间

Six bad safety habits in the development of enterprise digitalization, each of which is very dangerous!

Beijing internal promotion | the machine learning group of Microsoft Research Asia recruits full-time researchers in nlp/ speech synthesis and other directions
随机推荐
请问下为啥有的表写sql能查到数据,但在数据地图里查不到啊,查表结构也搜不到
Cmake tutorial Step3 (requirements for adding libraries)
Accuracy of BigDecimal Division
Action avant ou après l'enregistrement du message teamcenter
QT console printout
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
Size_t 是无符号的
Cartoon: a bloody case caused by a math problem
Vulnerability recurrence - 48. Command injection in airflow DAG (cve-2020-11978)
7 pratiques devops pour améliorer la performance des applications
一文读懂简单查询代价估算
Is it safe to open an account online? What is the general interest rate of securities financing?
C # mixed graphics and text, written to the database in binary mode
北京内推 | 微软亚洲研究院机器学习组招聘NLP/语音合成等方向全职研究员
论文阅读_中文NLP_LTP
Why is February 28 in the Gregorian calendar
2022新版PMP考试有哪些变化?
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
2022年信息系统管理工程师考试大纲
Cmake tutorial Step4 (installation and testing)