当前位置:网站首页>qt json
qt json
2022-06-29 12:48:00 【Mayfly wing*】
Simple introduction
json The basic rule :
- Data usage name / Value pairs indicate .
- Use braces to save objects , Each name is followed by ‘:’( The colon ), name / Value for use ,( comma ) Division .
- Save the array with square brackets , Array values use ,( comma ) Division .
class QJsonValue
For encapsulation JSON value
class QJsonObject
Responsible for packaging JSON object , It's a key / List of value pairs , Where key is the only string , Values are determined by QJsonValue Express .
Initialization mode :
QJsonObject jsonObject;
jsonObject["key1"] = 1;
jsonObject["key2"] = 6.6;
jsonObject.insert("key3", "Hello world");
jsonObject["array"] = QJsonArray({
1, 2, 3});
// perhaps
QJsonObject jsonObject
{
{
"key1", 1},
{
"key2", 6.6},
{
"key3", "Hello world"},
{
"array", QJsonArray({
1, 2, 3})}
};
QJsonArray
Responsible for packaging JSON Array ,JSON An array is a list of values , Interface and QVariantList similar ,QJsonArray And QVariantList Can be converted to each other .
QJsonList Operate on QList be similar , All have size()、insert() and removeAt() Wait for the operation , You can also use standard C++ The iterator pattern iterates over its contents .
Direct assignment uses :
QJsonArray jsonArray = {
1, 6.6, QString("Hello world") };
Interface operation use :
QJsonArray jsonArray;
jsonArray.append(1);
jsonArray.append(6.6);
jsonArray.insert(2, "Hello world");
QJsonDocument
// Use QJsonDocument Set the json object
QJsonDocument jsonDoc;
jsonDoc.setObject(jsonObject);
// take json Write the file as text and close the file .
file.write(jsonDoc.toJson());
file.close();
practice
{
"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
]
}
among "Page" The value of is a QJsonObject object ,"Version" It's a list
// structure Json Array - Version
QJsonArray versionArray;
versionArray.append(4.8);
versionArray.append(5.2);
versionArray.append(5.7);
// structure Json object - 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/");
// structure Json object
QJsonObject json;
json.insert("Name", "Qt");
json.insert("Company", "Digia");
json.insert("From", 1991);
json.insert("Version", QJsonValue(versionArray));
json.insert("Page", QJsonValue(pageObject));
// structure Json file
QJsonDocument document;
document.setObject(json);
QByteArray byteArray = document.toJson(QJsonDocument::Compact);
QString strJson(byteArray);
qDebug() << strJson;
QString and QJsonObject The mutual transformation of
// 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());
}
边栏推荐
- Baidu cloud disk downloads large files without speed limit (valid for 2021-11 personal test)
- Gbase8s database select has a having clause
- Gbase8s database into temp clause creates a temporary table to save query results.
- ZALSM_EXCEL_TO_INTERNAL_TABLE 导入数据大问题解决
- 【综合案例】信用卡虚拟交易识别
- JVM之方法区
- 2022.6.28-----leetcode. three hundred and twenty-four
- MFC-对话框程序核心-IsDialogMessage函数-MSG 消息结构-GetMessage函数-DispatchMessage函数
- NvtBack
- After class assignment of module 5 of the construction practice camp
猜你喜欢

How can colleges and universities build future oriented smart campus based on cloud native? Full stack cloud native architecture vs traditional IT architecture

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

百度云盘不限速下载大文件(2021-11亲测有效)

Quick look | the long-awaited 2022 Guangzhou assistant testing engineer's real problem analysis is finally released

QQ group was stolen, a large-scale social death scene caught off guard

Recurrence of recommended models (III): recall models youtubednn and DSSM

File contained log poisoning (user agent)

Go Senior Engineer required course | I sincerely suggest you listen to it. Don't miss it~

Unexpected ‘debugger‘ statement no-debugger

参加2022年杭州站Cocos Star Meetings
随机推荐
Huffman coding
ZALSM_EXCEL_TO_INTERNAL_TABLE 导入数据大问题解决
Gbase8s database into table clause
Gbase8s database select has order by Clause 6
asp.net 项目使用aspnet_compiler.exe发布
Yunlong fire version aircraft battle (full version)
How to create new user for ORACLE 19c (CDB & PDB)
How to fix ora-01017: invalid user name / password login denied
How to create new user for ORACLE 19c (CDB & PDB)
Paper reproduction - ac-fpn:attention-guided context feature pyramid network for object detection
Proteus软件初学笔记
Go Senior Engineer required course | I sincerely suggest you listen to it. Don't miss it~
go 学习-搭建开发环境vscode开发环境golang
Unexpected ‘debugger‘ statement no-debugger
LeetCode_双指针_中等_328.奇偶链表
C#通过中序遍历对二叉树进行线索化
MFC dialog program core -isdialogmessage function -msg message structure -getmessage function -dispatchmessage function
多项目开发入门-业务场景关联基础入门测试 工资表
oracle 19c : change the user sys/system username pasword under Linux
[Junzheng T31] decompression and packaging of read-only rootfs file system squashfs