当前位置:网站首页>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边栏推荐
- Binary tree problems
- The principle and source code analysis of Lucene index construction
- Building web automation environment
- Leetcode daily question - 522 Longest special sequence II
- ThreadLocal principle
- QT 一个控件的坐标怎么相对固定显示在另一个控件上(坐标系)
- Leetcode 36. Effective Sudoku (yes, once)
- Flask——总结
- Pyechart drawing multiple Y-axis line graphs
- Workplace tips | understanding the advantages of the position "knowing people"
猜你喜欢

The further application of Li Kou tree

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

力扣树的进一步应用
![[Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)](/img/cd/be62272d465ca990456c222b38df67.png)
[Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)
![[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)](/img/cd/be62272d465ca990456c222b38df67.png)
[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)

【筆記:模擬MOS集成電路】帶隙基准(基本原理+電流模+電壓模電路詳解)

【笔记:模拟MOS集成电路】带隙基准(基本原理+电流模+电压模电路详解)

Bitbucket 使用 SSH 拉取仓库失败的问题

How do I download videos? Look at the super simple method!

Automatic operation and maintenance platform based on Apache APIs
随机推荐
精通数据分析能力,收入翻倍?什么才是最强竞争力
Leetcode daily question - 30 Concatenate substrings of all words
LeetCode122. 买卖股票的最佳时机II
Leetcode daily question - 522 Longest special sequence II
题解 The Blocks Problem(UVa101)紫书P110vector的应用
RT thread thread synchronization and thread communication
postman简介与安装步骤
Embedded dynamic Arabic string conversion LCD display string [thanks for Jianguo ambition]
Flask——总结
ID access card copied to mobile phone_ How to turn a mobile phone into an access card mobile NFC copy access card graphic tutorial
Web 自动化环境搭建
No module named ‘PyEMD‘ ; Use plt figure()TypeError: ‘module‘ object is not callable
LeetCode121. 买卖股票的最佳时机
LeetCode123. The best time to buy and sell stocks III
接口用例设计
Is the inter-bank certificate of deposit reliable and safe
Learn Tai Chi maker mqtt Chapter 2 (VIII) esp8266 mqtt user password authentication
[learning notes] factor analysis
How to analyze the relationship between enterprise digital transformation and data asset management?
2. integrate filter