当前位置:网站首页>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() ";
}
}
};
边栏推荐
- 苹果手机炒股安全吗?打新债是骗局吗?
- 星环科技重磅推出数据要素流通平台Transwarp Navier,助力企业实现隐私保护下的数据安全流通与协作
- ConvMAE(2022-05)
- Electron installation problems
- English sentence pattern reference
- Logical words in Articles
- Penetrate the whole intranet through socks agent
- [paddlepaddle] paddedetection face recognition custom data set
- 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
- node_exporter内存使用率不显示
猜你喜欢
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
LeetCode 6109. 知道秘密的人数
ViewPager + RecyclerView的内存泄漏
@Extension, @spi annotation principle
About Estimation with Cross-Validation
Privacy computing helps secure data circulation and sharing
Sophon CE Community Edition is online, and free get is a lightweight, easy-to-use, efficient and intelligent data analysis tool
Image classification, just look at me!
吳恩達團隊2022機器學習課程,來啦
《2022中国信创生态市场研究及选型评估报告》发布 华云数据入选信创IT基础设施主流厂商!
随机推荐
GIMP 2.10教程「建议收藏」
MATLAB中print函数使用
Star Ring Technology launched transwarp Navier, a data element circulation platform, to help enterprises achieve secure data circulation and collaboration under privacy protection
图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
Login and connect CDB and PDB
Record a case of using WinDbg to analyze memory "leakage"
ConvMAE(2022-05)
Privacy computing helps secure data circulation and sharing
Problems encountered in the project u-parse component rendering problems
数值计算方法 Chapter8. 常微分方程的数值解
sample_rate(采樣率),sample(采樣),duration(時長)是什麼關系
【在优麒麟上使用Electron开发桌面应】
Let more young people from Hong Kong and Macao know about Nansha's characteristic cultural and creative products! "Nansha kylin" officially appeared
Cronab log: how to record the output of my cron script
Introduction to Resampling
Wu Enda team 2022 machine learning course, coming
【HCIA-cloud】【1】云计算的定义、什么是云计算、云计算的架构与技术说明、华为云计算产品、华为内存DDR配置工具说明
Crontab 日志:如何记录我的 Cron 脚本的输出
南京大学:新时代数字化人才培养方案探讨
Numerical calculation method chapter8 Numerical solutions of ordinary differential equations