当前位置:网站首页>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() ";
}
}
};
边栏推荐
- node_ Exporter memory usage is not displayed
- node_exporter内存使用率不显示
- 生词生词生词生词[2]
- 快速生成ipa包
- The origin of PTS, DTS and duration of audio and video packages
- Star Ring Technology launched transwarp Navier, a data element circulation platform, to help enterprises achieve secure data circulation and collaboration under privacy protection
- GIMP 2.10教程「建议收藏」
- U-Net: Convolutional Networks for Biomedical Images Segmentation
- Personal understanding of convolutional neural network
- 第十届全球云计算大会 | 华云数据荣获“2013-2022十周年特别贡献奖”
猜你喜欢
Pytorch yolov5 training custom data
Fix vulnerability - mysql, ES
【HCIA-cloud】【1】云计算的定义、什么是云计算、云计算的架构与技术说明、华为云计算产品、华为内存DDR配置工具说明
Find the first k small element select_ k
Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]
图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
U-Net: Convolutional Networks for Biomedical Images Segmentation
Sophon CE社区版上线,免费Get轻量易用、高效智能的数据分析工具
To solve the stubborn problem of Lake + warehouse hybrid architecture, xinghuan Technology launched an independent and controllable cloud native Lake warehouse integrated platform
@Extension、@SPI注解原理
随机推荐
Penetrate the whole intranet through socks agent
【在优麒麟上使用Electron开发桌面应】
Sophon autocv: help AI industrial production and realize visual intelligent perception
【在優麒麟上使用Electron開發桌面應】
Introduction to Resampling
Use JMeter to record scripts and debug
What is the reason why the video cannot be played normally after the easycvr access device turns on the audio?
Whether to take a duplicate subset with duplicate elements [how to take a subset? How to remove duplicates?]
Trust counts the number of occurrences of words in the file
About Estimation with Cross-Validation
ConvMAE(2022-05)
记一次使用Windbg分析内存“泄漏”的案例
Can communication of nano
[paddlepaddle] paddedetection face recognition custom data set
Xiaobai getting started with NAS - quick building private cloud tutorial series (I) [easy to understand]
Einstein sum einsum
消除`if()else{ }`写法
Matlab built-in function how different colors, matlab subsection function different colors drawing
【PaddleClas】常用命令
FCN: Fully Convolutional Networks for Semantic Segmentation