当前位置:网站首页>Usage example of qjsonobject
Usage example of qjsonobject
2022-06-28 21:18:00 【Licht powder】
Introduce : Responsible for packaging JSON object , yes key / It's worth it list , among Key is unique String , Values are determined by QJsonValue Express .
1——QJsonObjec: Encapsulates the Json The object in
Interface and QMap be similar , All have size()、insert() and remove() Wait for the operation , You can also use standards C++ The iterator pattern iterates over its contents .
Examples of use :
As shown in the figure below : If we want to write this data , How to write it in ?

QJsonObject gObj = JSONDATA.getSpecifyGroup(m_iSelectBtnIndex);
// Here read the first QJsonObject object , You can see it yourself according to your needs
// Next, we will read it layer by layer
// New addition effect
QJsonArray chValueArray;// This is an arbitrary array to be added , It's useful to
for(int i = 0 ; i < ui->cbLightChanleNum->currentText().toInt() ; i++)
{
chValueArray.append(QJsonValue(0));
}
QJsonObject eObj = gObj[JSONKEY::EFFECT_JSONKEY].toObject() ;
QJsonArray pArry = eObj[JSONKEY::PROGRAM_JSONKEY].toArray();
for(int i=0; i<pArry.size();i++)
{
QJsonObject eObject = pArry.at(i).toObject();
for(int i = 0; i < addrArray.size(); i++)
{
eObject.insert(QString::number(addrArray.at(i).toInt()),QJsonValue(chValueArray));
}
pArry.replace(i,eObject);
}
eObj[JSONKEY::PROGRAM_JSONKEY] = pArry;
gObj[JSONKEY::EFFECT_JSONKEY] = eObj;
JSONDATA.setSpecifyGroup(m_iSelectBtnIndex,gObj);// Here is the encapsulated deposit Json File interface
Just remember a little ,[] The one in front is Json Array Format ,{} That's it. Json Object format
2——QJsonArray: Encapsulates the Json Array in
3——QJsonDocument: converter
Known above JSON There are two formats , namely JSON Array and JSON object , They will eventually take advantage of JSON Complete serialization , Convert the data to the corresponding string , Or there is another case where a string is converted to JSON Array and JSON object .

ordinary JSON object
{
"Cross Platform": true,
"From": 1991,
"Name": "Qt"
}
Generate :
// structure JSON object
QJsonObject json;
json.insert("Name", "Qt");
json.insert("From", 1991);
json.insert("Cross Platform", true);
// structure JSON file
QJsonDocument document;
document.setObject(json);
QByteArray byteArray = document.toJson(QJsonDocument::Compact);
QString strJson(byteArray);
qDebug() << strJson;
ordinary JSON analysis
QJsonParseError jsonError;
// Turn into JSON file
QJsonDocument doucment = QJsonDocument::fromJson(byteArray, &jsonError);
// No error occurred in parsing
if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError))
{
if (doucment.isObject())
{
// JSON Document as object
QJsonObject object = doucment.object(); // Convert to object
if (object.contains("Name"))
{ // Contains the specified key
QJsonValue value = object.value("Name"); // Get specified key Corresponding value
if (value.isString())
{ // Judge value Is it a string
QString strName = value.toString(); // take value Convert to string
qDebug() << "Name : " << strName;
}
}
if (object.contains("From"))
{
QJsonValue value = object.value("From");
if (value.isDouble())
{
int nFrom = value.toVariant().toInt();
qDebug() << "From : " << nFrom;
}
}
if (object.contains("Cross Platform"))
{
QJsonValue value = object.value("Cross Platform");
if (value.isBool())
{
bool bCrossPlatform = value.toBool();
qDebug() << "CrossPlatform : " << bCrossPlatform;
}
}
}
}
4——QJsonValue: It is the packaging of the data type previously transferred ,C++ There are different declarations for different data types in ,QJsonValue Class is using constructors All kinds of data types are unified , Packed in one type , It's easier to operate .
QJsonValue Can be encapsulated There are six basic data types :
| Boolean type | QJsonValue::Bool |
| Floating point type ( Including plastic surgery ) | QJsonValue::Double |
| String type | QJsonValue::String |
| Json An array type | QJsonValue::Array |
| Json object type | QJsonValue::Object |
| Null type | QJsonValue::Null |
(1) Various types can be accessed through QJsonValue The constructor of is encapsulated as a class object ;
(2) If we get one QJsonValue object , How to determine what type of data is encapsulated inside ?
// Whether it is Json Array
bool isArray() const;
// Whether it is Json object
bool isObject() const;
// Whether it is Boolean
bool isBool() const;
// Whether it is a floating point type ( Shaping is also judged by this function )
bool isDouble() const;
// Whether it is a null value type
bool isNull() const;
// Is it a string type
bool isString() const;
// Whether it is an undefined type ( Unrecognized type )
bool isUndefined() const;
(3) After getting the actual type of the internal data of the object through the judgment function , If necessary, it can be converted to the corresponding basic data type again .
// Convert to Json Array
QJsonArray toArray(const QJsonArray &defaultValue) const;
QJsonArray toArray() const;
// Convert to boolean type
bool toBool(bool defaultValue = false) const;
// Convert to floating point type
double toDouble(double defaultValue = 0) const;
// Convert to plastic
int toInt(int defaultValue = 0) const;
// Convert to Json object
QJsonObject toObject(const QJsonObject &defaultValue) const;
QJsonObject toObject() const;
// Convert to string type
QString toString() const;
QString toString(const QString &defaultValue) const;
- QJsonValue And QVariant Interturn
QJsonValue fromVariant(const QVariant &variant)
QVariant QJsonValue::toVariant() const
- QJsonValue It can be done with QJsonObject,QJsonArray Interturn
QJsonValue::QJsonValue(const QJsonArray &a)
QJsonObject QJsonValue::toObject() const
QJsonValue::QJsonValue(const QJsonObject &o)
QJsonArray QJsonValue::toArray() const边栏推荐
- 学习太极创客 — MQTT 第二章(七)ESP8266 MQTT 遗嘱应用
- 什么是接口?什么是接口测试?
- 数据资产为王,如何解析企业数字化转型与数据资产管理的关系?
- 基于 Apache APISIX 的自动化运维平台
- Leetcode daily question - 522 Longest special sequence II
- Figure neural network can also be used as CV backbone model. Huawei Noah Vig architecture is comparable to CNN and transformer
- Anr no response introduction
- LeetCode877. Stone game
- 二叉树类题目 力扣
- LeetCode226. Flip binary tree
猜你喜欢

Lucene构建索引的原理及源代码分析

数据资产为王,如何解析企业数字化转型与数据资产管理的关系?

Apisik helps Middle East social software realize localized deployment

如何使用 DataAnt 监控 Apache APISIX

RT thread thread synchronization and thread communication

QJsonObject的使用示例

API gateway Apache APIs IX helps the evolution of snowball dual active architecture

Bitbucket failed to pull the warehouse Using SSH

APISIX 助力中东社交软件,实现本地化部署

Alibaba cloud MSE full link grayscale solution practice based on Apache apisik
随机推荐
Apisik helps Middle East social software realize localized deployment
[Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)
LeetCode117. 填充每个节点的下一个右侧节点指针_II
ref属性,props配置,mixin混入,插件,scoped样式
Proficient in data analysis, double the income? What is the strongest competitiveness
Is it safe to open a dig money account? Is it reliable?
开通挖财账号安全吗?是靠谱的吗?
Leetcode: merge two ordered linked lists_ twenty-one
Flatten of cnn-lstm
Interface use case design
Leetcode daily question - 324 Swing sort II
[try to hack] cobalt strike (I)
LeetCode188. 买卖股票的最佳时机IV
Alibaba cloud MSE full link grayscale solution practice based on Apache apisik
Understanding web automated testing
Ehcache configuration data, convenient for self checking
LeetCode877. 石子游戏
The blocks problem (uva101) Purple Book p110vector application
阿里云 MSE 基于 Apache APISIX 的全链路灰度方案实践
二叉树类题目 力扣