当前位置:网站首页>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 : 1complete 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() ";
}
}
};边栏推荐
- [paddlepaddle] paddedetection face recognition custom data set
- Writing writing writing
- 个人对卷积神经网络的理解
- 项目中遇到的问题 u-parse 组件渲染问题
- 访问数据库使用redis作为mysql的缓存(redis和mysql结合)
- Sophon KG升级3.1:打破数据间壁垒,解放企业生产力
- Trust counts the number of occurrences of words in the file
- @Extension, @spi annotation principle
- [paddleclas] common commands
- 消除`if()else{ }`写法
猜你喜欢

彻底理解为什么网络 I/O 会被阻塞?

U-Net: Convolutional Networks for Biomedical Images Segmentation

About Estimation with Cross-Validation

vulnhub之darkhole_2

ViewPager + RecyclerView的内存泄漏

Share: ZTE Yuanhang 30 Pro root unlock BL magick ZTE 7532n 8040n 9041n brush mask original brush package root method Download

第十一届中国云计算标准和应用大会 | 华云数据成为全国信标委云计算标准工作组云迁移专题组副组长单位副组长单位

Sophon CE社区版上线,免费Get轻量易用、高效智能的数据分析工具

Failed to virtualize table with JMeter

LeetCode 6111. 螺旋矩阵 IV
随机推荐
Penetrate the whole intranet through socks agent
Personal understanding of convolutional neural network
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
pytorch yolov5 训练自定义数据
sample_rate(采樣率),sample(采樣),duration(時長)是什麼關系
最大人工岛[如何让一个连通分量的所有节点都记录总节点数?+给连通分量编号]
Login and connect CDB and PDB
Vulnhub's darkhole_ two
ConvMAE(2022-05)
Notes on common management commands of openshift
Image classification, just look at me!
[QNX Hypervisor 2.2用户手册]6.3.2 配置VM
Is it safe to open an account and register stocks for stock speculation? Is there any risk? Is it reliable?
Nacos distributed transactions Seata * * install JDK on Linux, mysql5.7 start Nacos configure ideal call interface coordination (nanny level detail tutorial)
Sophon base 3.1 launched mlops function to provide wings for the operation of enterprise AI capabilities
兄弟组件进行传值(显示有先后顺序)
金太阳开户安全吗?万一免5开户能办理吗?
How can cluster deployment solve the needs of massive video access and large concurrency?
【HCIA-cloud】【1】云计算的定义、什么是云计算、云计算的架构与技术说明、华为云计算产品、华为内存DDR配置工具说明
[utiliser Electron pour développer le Bureau sur youkirin devrait]