当前位置:网站首页>QJsonObject的使用示例
QJsonObject的使用示例
2022-06-28 21:08:00 【Licht小粉】
介绍:负责封装JSON对象,是键/值对列表,其中键是惟一的字符串,值由QJsonValue表示。
1——QJsonObjec:封装了Json里的对象
接口与QMap相似,都具有size()、insert()和remove()等操作,还可以使用标准C++迭代器模式对其内容进行迭代。
使用示例:
如下图所示:我们要写入这个数据的话,怎么写进去?

QJsonObject gObj = JSONDATA.getSpecifyGroup(m_iSelectBtnIndex);
//这里读取第一个QJsonObject对象,你可以按照你的需求自己去看
//接下来我们就一层一层的读进去就可以了
//新增加effect
QJsonArray chValueArray;//这个随便写个要填加的数组,后面有用到
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);//这里是封装的存入Json文件的接口
记住一点就可以,[]前面的就是 Json数组格式,{}就是个Json对象格式
2——QJsonArray: 封装了Json里的数组
3——QJsonDocument:转换器
上文知JSON一共有两种格式,即JSON数组和JSON对象,它们最终要利用JSON完成序列化,将数据转换为对应的字符串,或者有另外一种情况是字符串转换为JSON数组和JSON对象。

简单的JSON对象
{
"Cross Platform": true,
"From": 1991,
"Name": "Qt"
}
生成:
// 构建 JSON 对象
QJsonObject json;
json.insert("Name", "Qt");
json.insert("From", 1991);
json.insert("Cross Platform", true);
// 构建 JSON 文档
QJsonDocument document;
document.setObject(json);
QByteArray byteArray = document.toJson(QJsonDocument::Compact);
QString strJson(byteArray);
qDebug() << strJson;
简单的JSON解析
QJsonParseError jsonError;
// 转化为 JSON 文档
QJsonDocument doucment = QJsonDocument::fromJson(byteArray, &jsonError);
// 解析未发生错误
if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError))
{
if (doucment.isObject())
{
// JSON 文档为对象
QJsonObject object = doucment.object(); // 转化为对象
if (object.contains("Name"))
{ // 包含指定的 key
QJsonValue value = object.value("Name"); // 获取指定 key 对应的 value
if (value.isString())
{ // 判断 value 是否为字符串
QString strName = value.toString(); // 将 value 转化为字符串
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: 是对前面调到的数据类型的包装,C++中针对不同的数据类型有不同的声明,QJsonValue类是利用构造函数将各种数据类型进行了统一化,包装成一种类型,操作更为简单。
QJsonValue 可以封装的基础数据类型有六种:
| 布尔类型 | QJsonValue::Bool |
| 浮点类型(包括整形) | QJsonValue::Double |
| 字符串类型 | QJsonValue::String |
| Json 数组类型 | QJsonValue::Array |
| Json 对象类型 | QJsonValue::Object |
| 空值类型 | QJsonValue::Null |
(1)各种类型可以通过 QJsonValue 的构造函数被封装为一个类对象;
(2)如果我们得到一个 QJsonValue 对象,如何判断内部封装的到底是什么类型的数据呢?
// 是否是Json数组
bool isArray() const;
// 是否是Json对象
bool isObject() const;
// 是否是布尔类型
bool isBool() const;
// 是否是浮点类型(整形也是通过该函数判断)
bool isDouble() const;
// 是否是空值类型
bool isNull() const;
// 是否是字符串类型
bool isString() const;
// 是否是未定义类型(无法识别的类型)
bool isUndefined() const;
(3)通过判断函数得到对象内部数据的实际类型之后,如果有需求就可以再次将其转换为对应的基础数据类型。
// 转换为Json数组
QJsonArray toArray(const QJsonArray &defaultValue) const;
QJsonArray toArray() const;
// 转换为布尔类型
bool toBool(bool defaultValue = false) const;
// 转换为浮点类型
double toDouble(double defaultValue = 0) const;
// 转换为整形
int toInt(int defaultValue = 0) const;
// 转换为Json对象
QJsonObject toObject(const QJsonObject &defaultValue) const;
QJsonObject toObject() const;
// 转换为字符串类型
QString toString() const;
QString toString(const QString &defaultValue) const;
- QJsonValue与QVariant互转
QJsonValue fromVariant(const QVariant &variant)
QVariant QJsonValue::toVariant() const
- QJsonValue可以与QJsonObject,QJsonArray互转
QJsonValue::QJsonValue(const QJsonArray &a)
QJsonObject QJsonValue::toObject() const
QJsonValue::QJsonValue(const QJsonObject &o)
QJsonArray QJsonValue::toArray() const边栏推荐
- openGauss内核分析之查询重写
- LeetCode117. 填充每个节点的下一个右侧节点指针_II
- The blocks problem (uva101) Purple Book p110vector application
- 学习太极创客 — MQTT 第二章(八)ESP8266 MQTT 用户密码认证
- MongoDB——副本集与分片
- 认识Web自动化测试
- How to add logs to debug anr problems
- LeetCode877. 石子游戏
- 如何添加 logs来debug ANR 问题
- How to open an account in great wisdom? Is it safe
猜你喜欢

力扣树的进一步应用

ThreadLocal principle

Alibaba cloud MSE full link grayscale solution practice based on Apache apisik

How to analyze the relationship between enterprise digital transformation and data asset management?

学习太极创客 — MQTT 第二章(七)ESP8266 MQTT 遗嘱应用

with torch.no_grad():的使用原因

ref属性,props配置,mixin混入,插件,scoped样式

Leetcode daily question - 515 Find the maximum value in each tree row

I almost ran away

Figure neural network can also be used as CV backbone model. Huawei Noah Vig architecture is comparable to CNN and transformer
随机推荐
认识Web自动化测试
How to do a good job in customer's successful bottom design | tob Master Course
LeetCode986. 区间列表的交集
LeetCode121. 买卖股票的最佳时机
LeetCode122. 买卖股票的最佳时机II
LeetCode188. The best time to buy and sell stocks IV
RT thread thread synchronization and thread communication
[try to hack] cobalt strike (I)
Leetcode 36. 有效的数独(可以,一次过)
接口用例设计
Ref attribute, props configuration, mixin mixing, plug-in, scoped style
在哪个软件上开户比较安全,开户流程是什么?
Is the rapid SSL wildcard certificate genuine in 1981
On the complexity of software development and the way to improve its efficiency
How to use dataant to monitor Apache apisex
如何添加 logs来debug ANR 问题
应用实践 | 10 亿数据秒级关联,货拉拉基于 Apache Doris 的 OLAP 体系演进(附 PPT 下载)
LeetCode117. 填充每个节点的下一个右侧节点指针_II
Lucene构建索引的原理及源代码分析
List of domestic database directory