当前位置:网站首页>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() ";
}
}
};
边栏推荐
- 吴恩达团队2022机器学习课程,来啦
- Sophon autocv: help AI industrial production and realize visual intelligent perception
- 兄弟组件进行传值(显示有先后顺序)
- 【在優麒麟上使用Electron開發桌面應】
- The 10th global Cloud Computing Conference | Huayun data won the "special contribution award for the 10th anniversary of 2013-2022"
- How to obtain the coordinates of the aircraft passing through both ends of the radar
- Let more young people from Hong Kong and Macao know about Nansha's characteristic cultural and creative products! "Nansha kylin" officially appeared
- Simulate the hundred prisoner problem
- Is it safe to open an account, register and dig money? Is there any risk? Is it reliable?
- 瞅一瞅JUC提供的限流工具Semaphore
猜你喜欢
About Estimation with Cross-Validation
About statistical power
The 10th global Cloud Computing Conference | Huayun data won the "special contribution award for the 10th anniversary of 2013-2022"
记录Pytorch中的eval()和no_grad()
图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
The 2022 China Xinchuang Ecological Market Research and model selection evaluation report released that Huayun data was selected as the mainstream manufacturer of Xinchuang IT infrastructure!
Sophon Base 3.1 推出MLOps功能,为企业AI能力运营插上翅膀
FCN: Fully Convolutional Networks for Semantic Segmentation
node_ Exporter memory usage is not displayed
The 11th China cloud computing standards and Applications Conference | China cloud data has become the deputy leader unit of the cloud migration special group of the cloud computing standards working
随机推荐
Nacos distributed transactions Seata * * install JDK on Linux, mysql5.7 start Nacos configure ideal call interface coordination (nanny level detail tutorial)
[paddleclas] common commands
Sophon Base 3.1 推出MLOps功能,为企业AI能力运营插上翅膀
Le cours d'apprentissage de la machine 2022 de l'équipe Wunda arrive.
Problems encountered in the project u-parse component rendering problems
Generate classes from XML schema
Eliminate the writing of 'if () else{}'
[utiliser Electron pour développer le Bureau sur youkirin devrait]
Thoroughly understand why network i/o is blocked?
What is the reason why the video cannot be played normally after the easycvr access device turns on the audio?
Xiaobai getting started with NAS - quick building private cloud tutorial series (I) [easy to understand]
在通达信上做基金定投安全吗?
个人对卷积神经网络的理解
Failed to virtualize table with JMeter
《力扣刷题计划》复制带随机指针的链表
RPC协议详解
开户注册股票炒股安全吗?有没有风险的?靠谱吗?
Share: ZTE Yuanhang 30 Pro root unlock BL magick ZTE 7532n 8040n 9041n brush mask original brush package root method Download
Crontab 日志:如何记录我的 Cron 脚本的输出
Leetcode notes: Weekly contest 300