当前位置:网站首页>Use QT to traverse JSON documents and search sub objects
Use QT to traverse JSON documents and search sub objects
2022-07-05 18:25:00 【Black footprints_ DarkSpoor】
About qt Traverse Json The code of the document , Sort it out and record it here , Convenient for future use .
QT The version of is shown in the figure below
Json The contents of the document :
{
" animal ": {
" people ": {
" Age ": 43,
" Telephone ": [
"+123 12345678",
"+321 87654321"
]
},
" Dog ": {
" leg ": [
" Left front leg ",
" Right front leg ",
" Left hind leg ",
" Right hind leg "
],
" head ": {
" eyes ": 2,
" nose ": 1,
" Ears ": 2
}
}
}
}
Use QJsonDocument The output of traversing all objects is as follows :
"" " root " isObject() Yes 1 Subobject
" " " animal " isObject() Yes 2 Subobject
" " " people " isObject() Yes 2 Subobject
" " " Age " isDouble() 43
" " " Telephone " isArray() Yes 2 Subobject
" " "" isString() "+123 12345678"
" " "" isString() "+321 87654321"
" " " Dog " isObject() Yes 2 Subobject
" " " head " isObject() Yes 3 Subobject
" " " eyes " isDouble() 2
" " " Ears " isDouble() 2
" " " nose " isDouble() 1
" " " leg " isArray() Yes 4 Subobject
" " "" isString() " Left front leg "
" " "" isString() " Right front leg "
" " "" isString() " Left hind leg "
" " "" isString() " Right hind leg "
Traverse and output Json Object code :
// load Json file
static void LoadJsonFromFile(const QString& jsonfilename,QJsonDocument& jsDoc)
{
QFile file(jsonfilename);
file.open(QIODevice::ReadWrite);
QByteArray json = file.readAll();
jsDoc = QJsonDocument::fromJson(json);
}
// Traverse the output Json object
// Input is Json file
static void PrintJson(QJsonDocument& jDoc)
{
if(jDoc.isObject())
{
QJsonObject jObject = jDoc.object(); // Get root object
PrintJsonObject(jObject," root ");
}
else if(jDoc.isArray())
{
QJsonArray jArray = jDoc.array();
PrintJsonArray(jArray,"");
}
else
{
// empty document
qDebug() << " empty document ";
}
}
// Traverse QJsonObject
static void PrintJsonObject(QJsonObject& obj,const QString& key,int retract=0)
{
// Indent used for output
QString tabs = "";
for(int i=0;i<retract;++i) {tabs+=" ";}
qDebug() << tabs << key << "isObject() Yes " << obj.size() << " Subobject ";
for(const QString& key: obj.keys()) // Traverse all key
{
QJsonValue jvalue = obj[key];
PrintJsonValue(jvalue,key,retract+1); // Printout QJsonValue
}
}
// Traverse QJsonArray
static void PrintJsonArray(QJsonArray& arr,const QString& key,int retract=0)
{
// Indent used for output
QString tabs = "";
for(int i=0;i<retract;++i) {tabs+=" ";}
qDebug() << tabs << key << "isArray() Yes " << arr.size() << " Subobject ";
for(QJsonArray::iterator it=arr.begin();it!=arr.end();++it) // Traverse the objects in the array
{
QJsonValue jvalue = *it;
PrintJsonValue(jvalue,"",retract+1); // Printout QJsonValue
}
}
// Printout QJsonValue
static void PrintJsonValue(QJsonValue& jvalue,const QString& key,int retract=0)
{
// Indent used for output
QString tabs = "";
for(int i=0;i<retract;++i) {tabs+=" ";}
if(jvalue.isArray())
{
QJsonArray childArray = jvalue.toArray();
PrintJsonArray(childArray,key,retract); // Recursively call
}
else if(jvalue.isObject())
{
QJsonObject childObject = jvalue.toObject();
PrintJsonObject(childObject,key,retract); // Recursively call
}
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() ";
}
}
// Test code
QJsonDocument doc;
ZJsonTools::LoadJsonFromFile(QString("json Full path name of the file "),doc);
ZJsonTools::PrintJson(doc);
Search for Json Code for :
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;
}
// Test code
QStringList path = {" animal "," Dog "," head "," nose "};
QJsonObject jObject = doc.object();
QJsonValue jObj;
if(ZJsonTools::SearchJsonValue(jObject,path,jObj))
{
qDebug()<< " Number of dogs' noses : " << jObj.toInt();
}
else
{
qDebug()<< " Not found ";
}
Search for Json Test code output :
Number of dogs' noses : 1
complete ZJsonTools Class code :
#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," root ");
}
else if(jDoc.isArray())
{
QJsonArray jArray = jDoc.array();
PrintJsonArray(jArray,"");
}
else
{
// empty document
qDebug() << " empty document ";
}
}
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)
{
// Indent used for output
QString tabs = "";
for(int i=0;i<retract;++i) {tabs+=" ";}
qDebug() << tabs << key << "isObject() Yes " << obj.size() << " Subobject ";
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)
{
// Indent used for output
QString tabs = "";
for(int i=0;i<retract;++i) {tabs+=" ";}
qDebug() << tabs << key << "isArray() Yes " << arr.size() << " Subobject ";
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)
{
// Indent used for output
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() ";
}
}
};
边栏推荐
- Simulate the hundred prisoner problem
- node_ Exporter memory usage is not displayed
- 开户注册挖财安全吗?有没有风险的?靠谱吗?
- 记录Pytorch中的eval()和no_grad()
- 星环科技重磅推出数据要素流通平台Transwarp Navier,助力企业实现隐私保护下的数据安全流通与协作
- 开户注册股票炒股安全吗?有没有风险的?靠谱吗?
- About Estimation with Cross-Validation
- Login and connect CDB and PDB
- sample_ What is the relationship between rate, sample and duration
- Sophon autocv: help AI industrial production and realize visual intelligent perception
猜你喜欢
Sophon kg upgrade 3.1: break down barriers between data and liberate enterprise productivity
让更多港澳青年了解南沙特色文创产品!“南沙麒麟”正式亮相
星环科技数据安全管理平台 Defensor重磅发布
Fix vulnerability - mysql, ES
JVM third talk -- JVM performance tuning practice and high-frequency interview question record
buuctf-pwn write-ups (9)
Sophon Base 3.1 推出MLOps功能,为企业AI能力运营插上翅膀
瞅一瞅JUC提供的限流工具Semaphore
About Statistical Power(统计功效)
寻找第k小元素 前k小元素 select_k
随机推荐
彻底理解为什么网络 I/O 会被阻塞?
English sentence pattern reference
【PaddleClas】常用命令
Let more young people from Hong Kong and Macao know about Nansha's characteristic cultural and creative products! "Nansha kylin" officially appeared
ConvMAE(2022-05)
图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
Wu Enda team 2022 machine learning course, coming
The 11th China cloud computing standards and Applications Conference | cloud computing national standards and white paper series release, and Huayun data fully participated in the preparation
含重复元素取不重复子集[如何取子集?如何去重?]
Huaxia Fund: sharing of practical achievements of digital transformation in the fund industry
【PaddlePaddle】 PaddleDetection 人脸识别 自定义数据集
Thoroughly understand why network i/o is blocked?
Sophon CE社区版上线,免费Get轻量易用、高效智能的数据分析工具
[utiliser Electron pour développer le Bureau sur youkirin devrait]
吴恩达团队2022机器学习课程,来啦
vs2017 qt的各种坑
sample_rate(采样率),sample(采样),duration(时长)是什么关系
Multithreading (I) processes and threads
Image classification, just look at me!
瞅一瞅JUC提供的限流工具Semaphore