当前位置:网站首页>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());
}
边栏推荐
- 墨菲安全入选中关村科学城24个重点项目签约
- Gbase8s database sorts standard or raw result tables
- MySQL主从同步之 异步复制 半同步复制 全同步复制
- Factorization of large numbers ← C language
- Quick look | the long-awaited 2022 Guangzhou assistant testing engineer's real problem analysis is finally released
- Paper reproduction - ac-fpn:attention-guided context feature pyramid network for object detection
- 2022.6.28-----leetcode.324
- Inferiority complex and transcendence the meaning of life to you
- C#通过中序遍历对二叉树进行线索化
- 【君正T31】只读rootfs文件系统squashfs的解压和打包
猜你喜欢

Unexpected ‘debugger‘ statement no-debugger

Qt中的UI文件介绍

C#通过中序遍历对二叉树进行线索化

How to calculate win/tai/loss in paired t-test

模糊图片变清晰,一键双色图片,快速整理本地图片...这8个在线图片工具申请加入你的收藏夹!

Murphy safety was selected for signing 24 key projects of Zhongguancun Science City

1. opencv realizes simple color recognition

从Mpx资源构建优化看splitChunks代码分割

How to create new user for ORACLE 19c (CDB & PDB)

【JUC系列】同步工具类之ThreadLocal
随机推荐
解决问题:ModuleNotFoundError: No module named ‘pip‘
C#通过线索二叉树进行中序遍历输出
[intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial
深入理解 volatile 关键字
MySQL数据库主从同步,一致性解决方案
huffman编码
Pangolin编译error: ‘numeric_limits’ is not a member of ‘std’
Testing -- automated testing: about the unittest framework
535. TinyURL 的加密与解密 : 设计一个 URL 简化系统
内插散点数据
《自卑与超越》生活对你应有的意义
Problem solving: modulenotfounderror: no module named 'pip‘
Gbase8s database sorts standard or raw result tables
Gbase8s database select has order by Clause 5
【综合案例】信用卡虚拟交易识别
Gbase8s database into table clause
Lm07 - detailed discussion on cross section strategy of futures
模糊图片变清晰,一键双色图片,快速整理本地图片...这8个在线图片工具申请加入你的收藏夹!
Blurred pictures become clear, one button two-color pictures, quickly organize local pictures These 8 online picture tools apply to join your favorites!
LeetCode_ Double pointer_ Medium_ 328. parity linked list