当前位置:网站首页>qt json
qt json
2022-06-29 12:04:00 【蜉蝣之翼*】
- 数据使用名/值对表示。
- 使用大括号保存对象,每个名称后面跟着一个 ‘:’(冒号),名/值对使用 ,(逗号)分割。
- 使用方括号保存数组,数组值使用 ,(逗号)分割。
类QJsonValue
用于封装JSON值
类QJsonObject
负责封装JSON对象,是键/值对列表,其中键是惟一的字符串,值由QJsonValue表示。
初始化方式:
QJsonObject jsonObject;
jsonObject["key1"] = 1;
jsonObject["key2"] = 6.6;
jsonObject.insert("key3", "Hello world");
jsonObject["array"] = QJsonArray({
1, 2, 3});
//或者
QJsonObject jsonObject
{
{
"key1", 1},
{
"key2", 6.6},
{
"key3", "Hello world"},
{
"array", QJsonArray({
1, 2, 3})}
};
QJsonArray
负责封装JSON数组,JSON数组是一个值列表,接口与QVariantList类似,QJsonArray与QVariantList可以互相转换。
QJsonList操作于QList相似,都具有size()、insert()和removeAt()等操作,还可以使用标准的C++迭代器模式对其内容进行迭代。
直接赋值使用:
QJsonArray jsonArray = {
1, 6.6, QString("Hello world") };
接口操作使用:
QJsonArray jsonArray;
jsonArray.append(1);
jsonArray.append(6.6);
jsonArray.insert(2, "Hello world");
QJsonDocument
// 使用QJsonDocument设置该json对象
QJsonDocument jsonDoc;
jsonDoc.setObject(jsonObject);
// 将json以文本形式写入文件并关闭文件。
file.write(jsonDoc.toJson());
file.close();
练习
{
"Company": "Digia",
"From": 1991,
"Name": "Qt",
"Page": {
"Developers": "https://www.qt.io/developers/",
"Download": "https://www.qt.io/download/",
"Home": "https://www.qt.io/"
},
"Version": [
4.8,
5.2,
5.7
]
}
其中 "Page"的值是一个QJsonObject对象,"Version"是一个列表
// 构建 Json 数组 - Version
QJsonArray versionArray;
versionArray.append(4.8);
versionArray.append(5.2);
versionArray.append(5.7);
// 构建 Json 对象 - Page
QJsonObject pageObject;
pageObject.insert("Home", "https://www.qt.io/");
pageObject.insert("Download", "https://www.qt.io/download/");
pageObject.insert("Developers", "https://www.qt.io/developers/");
// 构建 Json 对象
QJsonObject json;
json.insert("Name", "Qt");
json.insert("Company", "Digia");
json.insert("From", 1991);
json.insert("Version", QJsonValue(versionArray));
json.insert("Page", QJsonValue(pageObject));
// 构建 Json 文档
QJsonDocument document;
document.setObject(json);
QByteArray byteArray = document.toJson(QJsonDocument::Compact);
QString strJson(byteArray);
qDebug() << strJson;
// QString >> QJson
QJsonObject getJsonObjectFromString(const QString jsonString){
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonString.toLocal8Bit().data());
if( jsonDocument.isNull() ){
qDebug()<< "===> please check the string "<< jsonString.toLocal8Bit().data();
}
QJsonObject jsonObject = jsonDocument.object();
return jsonObject;
}
// QJson >> QString
QString getStringFromJsonObject(const QJsonObject& jsonObject){
return QString(QJsonDocument(jsonObject).toJson());
}
边栏推荐
- 二十三、1-Bit数据的存储(延迟线/磁芯/DRAM/SRAM/磁带/磁盘/光盘/Flash SSD)
- ERP编制物料清单 金蝶
- 【云原生】2.4 Kubernetes 核心实战(中)
- GBase 8s 扩展外连接1
- MFC dialog program core -isdialogmessage function -msg message structure -getmessage function -dispatchmessage function
- 面试突击61:说一下MySQL事务隔离级别?
- Gbase8s database into standard and into raw clauses
- Artbench: the first class balanced, high-quality, clean annotated and standardized artwork generation data set
- 缓存一致性,删除缓存,写入缓存,缓存击穿,缓存穿透,缓存雪崩
- 《Go题库·14》WaitGroup的坑
猜你喜欢

Interview shock 61: tell me about MySQL transaction isolation level?
![[cloud native] 2.4 kubernetes core practice (middle)](/img/1e/b1b22caa03d499387e1a47a5f86f25.png)
[cloud native] 2.4 kubernetes core practice (middle)

Qt的信号与槽

After class assignment of module 5 of the construction practice camp

Introduction to multi project development - business scenario Association basic introduction test payroll

Paper reproduction - ac-fpn:attention-guided context feature pyramid network for object detection

Bison uses error loop records

oracle 19c : change the user sys/system username pasword under Linux

如何计算win/tai/loss in paired t-test

Cereal mall project
随机推荐
如何計算win/tai/loss in paired t-test
GBase8s数据库INTO STANDARD 和 INTO RAW 子句
ERP编制物料清单 金蝶
[JUC series] ThreadLocal of synchronization tool class
GBase8s数据库select有ORDER BY 子句4
MySQL 主从复制原理以及流程
《自卑与超越》生活对你应有的意义
深入理解 volatile 关键字
ArtBench:第一个类平衡的、高质量的、干净注释的和标准化的艺术品生成数据集
Weekly recommended short video: How did Einstein think?
Gbase8s database select has order by Clause 5
二十三、1-Bit数据的存储(延迟线/磁芯/DRAM/SRAM/磁带/磁盘/光盘/Flash SSD)
Go learning - build a development environment vscode development environment golang
Gbase8s database select has order by Clause 3
地球观测卫星数据
Understanding of P value
GBase8s数据库select有ORDER BY 子句6
Gbase8s database for read only clause
MFC-对话框程序核心-IsDialogMessage函数-MSG 消息结构-GetMessage函数-DispatchMessage函数
How to fix ora-01017: invalid user name / password login denied