柚子快報(bào)邀請(qǐng)碼778899分享:QT中Json類(lèi)應(yīng)用
目錄
概要:
一、QJsonValue類(lèi)
1.1?JSON基本數(shù)據(jù)類(lèi)型
1.2?QJsonValue成員方法
1.3 常用API函數(shù)
1.4 示例
二、QJsonObject類(lèi)
2.1 常用API
三、QJsonArray類(lèi)
3.1 常用API
3.2 成員方法解析
3.3 示例
3.3.1?Json數(shù)組的生成
3.3.2?Json數(shù)組生成和解析
四、QJsonDocument類(lèi)
4.1 常用API
4.2 應(yīng)用示例
五、其他示例
5.1 Write to File
5.2 Read from File
5.3 示例3
概要:
QT5.0開(kāi)始支持Json類(lèi),常用的四個(gè)類(lèi):QJsonValue、QJsonDocument、QJsonArray、QJsonObject。
Json類(lèi)介紹QJsonValue該類(lèi)封裝了JSON支持的數(shù)據(jù)類(lèi)型QJsonArrayJSON數(shù)組是一個(gè)值列表??梢酝ㄟ^(guò)從數(shù)組中插入和刪除QJsonValue來(lái)操作該列表。QJsonObjectJSON對(duì)象是鍵值對(duì)的列表,其中鍵是唯一的字符串,值由QJsonValue表示。QJsonDocument 它封裝了一個(gè)完整的JSON文檔,并且可以從UTF-8編碼的基于文本的表示以及Qt自己的二進(jìn)制格式讀取和寫(xiě)入該文檔。
一、QJsonValue類(lèi)
QJsonValue類(lèi)將一個(gè)值封裝在JSON中。用的是鍵值對(duì)去索引數(shù)據(jù)的,鍵就是字符串,值就是我們要用的數(shù)據(jù)。在Qt里用的是 QJsonValue 表示數(shù)據(jù)。
1.1?JSON基本數(shù)據(jù)類(lèi)型
在Qt中?QJsonValue?可以封裝的基礎(chǔ)數(shù)據(jù)類(lèi)型有六種(和Json支持的類(lèi)型一致),分別為:
布爾類(lèi)型:QJsonValue::Bool浮點(diǎn)類(lèi)型(包括整形):QJsonValue::Double字符串類(lèi)型:?QJsonValue::StringJson數(shù)組類(lèi)型:?QJsonValue::ArrayJson對(duì)象類(lèi)型:QJsonValue::Object空值類(lèi)型:?QJsonValue::Null
1.2?QJsonValue成員方法
各種類(lèi)型可以通過(guò) QJsonValue 的構(gòu)造函數(shù)被封裝為一個(gè)類(lèi)對(duì)象:
QJsonValue(QJsonValue::Type type = Null)
QJsonValue(bool b)
QJsonValue(double n)
QJsonValue(int n)
QJsonValue(qint64 n)
QJsonValue(const QString &s)
QJsonValue(QLatin1String s)
QJsonValue(const char *s)
QJsonValue(const QJsonArray &a)
QJsonValue(const QJsonObject &o)
QJsonValue(const QJsonValue &other)
QJsonValue(QJsonValue &&other)
~QJsonValue()
bool isArray() const
bool isBool() const
bool isDouble() const
bool isNull() const
bool isObject() const
bool isString() const
bool isUndefined() const
void swap(QJsonValue &other)
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
QJsonObject toObject(const QJsonObject &defaultValue) const
QJsonObject toObject() const
QString toString() const
QString toString(const QString &defaultValue) const
QVariant toVariant() const
QJsonValue::Type type() const
bool operator!=(const QJsonValue &other) const
QJsonValue &operator=(const QJsonValue &other)
QJsonValue &operator=(QJsonValue &&other)
bool operator==(const QJsonValue &other) const
const QJsonValue operator[](const QString &key) const
const QJsonValue operator[](QLatin1String key) const
const QJsonValue operator[](int i) const
1.3 常用API函數(shù)
// QJsonValue 常用api
1. isNull(): 檢查該值是否為 null。
2. isBool(): 檢查該值是否為 bool 類(lèi)型。
3. isDouble(): 檢查該值是否為 double 類(lèi)型。
4. isString(): 檢查該值是否為字符串類(lèi)型。
5. isObject(): 檢查該值是否為對(duì)象類(lèi)型。
6. isArray(): 檢查該值是否為數(shù)組類(lèi)型。
7. toInt(): 將該值轉(zhuǎn)換為整數(shù)類(lèi)型。
8. toDouble(): 將該值轉(zhuǎn)換為 double 類(lèi)型。
9. toString(): 將該值轉(zhuǎn)換為字符串類(lèi)型。
10. toObject(): 將該值轉(zhuǎn)換為對(duì)象類(lèi)型。
11. toArray(): 將該值轉(zhuǎn)換為數(shù)組類(lèi)型。
12. operator[](): 獲取數(shù)組或?qū)ο笾兄付ㄋ饕蜴I的值。
13. keys(): 獲取對(duì)象類(lèi)型的所有鍵。
14. size(): 獲取數(shù)組或?qū)ο箢?lèi)型的元素個(gè)數(shù)。
通過(guò)上述三種函數(shù)可以方便的將數(shù)據(jù)打包為QJsonValue類(lèi)型,后利用is方法判定數(shù)據(jù)類(lèi)型,最后利用to方法轉(zhuǎn)換為原始數(shù)據(jù)。
1.4 示例
#include
#include
#include
#include
// 創(chuàng)建一個(gè) JSON 值
QJsonValue value(42); // 從整數(shù)創(chuàng)建
// 檢查值的類(lèi)型
if (value.isDouble()) {
// 轉(zhuǎn)換為原始類(lèi)型
int num = value.toInt();
}
// 創(chuàng)建一個(gè) JSON 對(duì)象
QJsonObject obj;
obj["key"] = value; // 將 JSON 值添加到對(duì)象中
// 訪(fǎng)問(wèn) JSON 對(duì)象中的值
QJsonValue extractedValue = obj["key"];
二、QJsonObject類(lèi)
QJsonObject?封裝了Json中的對(duì)象,在里邊可以存儲(chǔ)多個(gè)鍵值對(duì),為了方便操作,鍵值為字符串類(lèi)型,值為QJsonValue?類(lèi)型。
2.1 常用API
// QJsonObject 的 API:
1. insert():向 QJsonObject 中插入一個(gè)鍵值對(duì)
2. remove():從 QJsonObject 中移除一個(gè)鍵值對(duì)
3. value():獲取 QJsonObject 中某個(gè)鍵的值
4. contains():判斷 QJsonObject 中是否包含某個(gè)鍵
5. keys():獲取 QJsonObject 中所有鍵的列表
6. size():獲取 QJsonObject 中鍵值對(duì)的數(shù)量
7. toVariant():將 QJsonObject 轉(zhuǎn)換為 QVariant 對(duì)象
8. fromVariant():將 QVariant 對(duì)象轉(zhuǎn)換為 QJsonObject 對(duì)象
9. take()是一個(gè)函數(shù),用于從QJsonObject中獲取并刪除指定鍵的值,返回該值。如果指定鍵不存在,則返回一個(gè)未初始化的QJsonValue對(duì)象。該函數(shù)的作用類(lèi)似于從字典中刪除一個(gè)鍵值對(duì),并返回該值。
關(guān)于這個(gè)類(lèi)的使用類(lèi)似于C++中的STL類(lèi),仔細(xì)閱讀API文檔即可熟練上手使用,下面介紹一些常用API函數(shù):
如何創(chuàng)建空的Json對(duì)象 QJsonObject::QJsonObject(); // 構(gòu)造空對(duì)象
將鍵值對(duì)添加到空對(duì)象中 iterator QJsonObject::insert(const QString &key, const QJsonValue &value);
獲取對(duì)象中鍵值對(duì)個(gè)數(shù) int QJsonObject::count() const;
int QJsonObject::size() const;
int QJsonObject::length() const;
通過(guò)key得到value QJsonValue QJsonObject::value(const QString &key) const; // utf8
QJsonValue QJsonObject::value(QLatin1String key) const; // 字符串不支持中文
QJsonValue QJsonObject::operator[](const QString &key) const;
QJsonValue QJsonObject::operator[](QLatin1String key) const;
刪除鍵值對(duì) void QJsonObject::remove(const QString &key);
QJsonValue QJsonObject::take(const QString &key); // 返回key對(duì)應(yīng)的value值
通過(guò)key進(jìn)行查找 iterator QJsonObject::find(const QString &key);
bool QJsonObject::contains(const QString &key) const;
遍歷,方式有三種:
使用相關(guān)的迭代器函數(shù) 使用 [] 的方式遍歷, 類(lèi)似于遍歷數(shù)組, []中是鍵值 先得到對(duì)象中所有的鍵值, 在遍歷鍵值列表, 通過(guò)key得到value值 QStringList QJsonObject::keys() const;
三、QJsonArray類(lèi)
QJsonArray?封裝了Json中的數(shù)組,?JSON數(shù)組是值的鏈表,可以插入和刪除QJsonValue。在里邊可以存儲(chǔ)多個(gè)元素,為了方便操作,所有的元素類(lèi)統(tǒng)一為?QJsonValue?類(lèi)型。關(guān)于這個(gè)類(lèi)的使用類(lèi)似于C++中的STL類(lèi),仔細(xì)閱讀API文檔即可熟練上手使用,下面介紹一些常用API函數(shù):? QJsonArray是一個(gè)隱式共享的類(lèi),只要沒(méi)有被改變,可以和創(chuàng)建QJsonArray的document共享數(shù)據(jù)。通過(guò)QJsonDocument可以將一個(gè)QJsonArray轉(zhuǎn)換成或轉(zhuǎn)換自一個(gè)文本形式的JSON。
3.1 常用API
QJsonArray()
QJsonArray(std::initializer_list
QJsonArray(const QJsonArray &other)
QJsonArray(QJsonArray &&other)
~QJsonArray()
void append(const QJsonValue &value)
QJsonValue at(int i) const
QJsonArray::iterator begin()
QJsonArray::const_iterator begin() const
QJsonArray::const_iterator constBegin() const
QJsonArray::const_iterator constEnd() const
bool contains(const QJsonValue &value) const
int count() const
bool empty() const
QJsonArray::iterator end()
QJsonArray::const_iterator end() const
QJsonArray::iterator erase(QJsonArray::iterator it)
QJsonValue first() const
void insert(int i, const QJsonValue &value)
QJsonArray::iterator insert(QJsonArray::iterator before, const QJsonValue &value)
bool isEmpty() const
QJsonValue last() const
void pop_back()
void pop_front()
void prepend(const QJsonValue &value)
void push_back(const QJsonValue &value)
void push_front(const QJsonValue &value)
void removeAt(int i)
void removeFirst()
void removeLast()
void replace(int i, const QJsonValue &value)
int size() const
void swap(QJsonArray &other)
QJsonValue takeAt(int i)
QVariantList toVariantList() const
bool operator!=(const QJsonArray &other) const
QJsonArray operator+(const QJsonValue &value) const
QJsonArray & operator+=(const QJsonValue &value)
QJsonArray & operator<<(const QJsonValue &value)
QJsonArray & operator=(const QJsonArray &other)
QJsonArray & operator=(QJsonArray &&other)
bool operator==(const QJsonArray &other) const
QJsonValueRef operator[](int i)
QJsonValue operator[](int i) const
3.2 成員方法解析
QJsonArray::QJsonArray(std::initializer_list
構(gòu)建一個(gè)QJsonArray
QJsonArray::QJsonArray(const QJsonArray &other)
void QJsonArray::append(const QJsonValue &value)
在QJsonArray尾部插入value
QJsonValue QJsonArray::at(int i) const
返回QJsonArray中索引為i的QJsonValue值
iterator QJsonArray::begin()
const_iterator QJsonArray::begin() const
返回指向數(shù)組第一個(gè)元素的STL風(fēng)格迭代器
const_iterator QJsonArray::constBegin() const
返回指向數(shù)組第一個(gè)元素的const STL風(fēng)格迭代器
const_iterator QJsonArray::constEnd() const
返回指向數(shù)組最后一個(gè)元素后的位置的const STL風(fēng)格迭代器
bool QJsonArray::contains(const QJsonValue &value) const
如果數(shù)組中包含value,返回true
int QJsonArray::count() const
返回?cái)?shù)組的大小
bool QJsonArray::empty() const
如果數(shù)組為空,返回true
const_iterator QJsonArray::end() const
返回指向數(shù)組最后一個(gè)元素后的位置的STL風(fēng)格迭代器
iterator QJsonArray::erase(iterator it)
刪除迭代器it指向的元素,返回指向下一個(gè)元素的迭代器
QJsonValue QJsonArray::first() const
返回?cái)?shù)組中的第一個(gè)值
[static] QJsonArray QJsonArray::fromStringList(const QStringList &list)
將一個(gè)字符串鏈表list轉(zhuǎn)換為QJsonArray
[static] QJsonArray QJsonArray::fromVariantList(const QVariantList &list)
將鏈表list轉(zhuǎn)換為QJsonArray
創(chuàng)建空的Json數(shù)組 QJsonArray::QJsonArray();
添加數(shù)據(jù) void QJsonArray::append(const QJsonValue &value); // 在尾部追加
void QJsonArray::insert(int i, const QJsonValue &value); // 插入到 i 的位置之前
iterator QJsonArray::insert(iterator before, const QJsonValue &value);
void QJsonArray::prepend(const QJsonValue &value); // 添加到數(shù)組頭部
void QJsonArray::push_back(const QJsonValue &value); // 添加到尾部
void QJsonArray::push_front(const QJsonValue &value); // 添加到頭部
計(jì)算數(shù)組元素的個(gè)數(shù) int QJsonArray::count() const;
int QJsonArray::size() const;
從數(shù)組中取出某一個(gè)元素的值 QJsonValue QJsonArray::at(int i) const;
QJsonValue QJsonArray::first() const; // 頭部元素
QJsonValue QJsonArray::last() const; // 尾部元素
QJsonValueRef QJsonArray::operator[](int i);
刪除數(shù)組中的某一個(gè)元素 iterator QJsonArray::erase(iterator it); // 基于迭代器刪除
void QJsonArray::pop_back(); // 刪除尾部
void QJsonArray::pop_front(); // 刪除頭部
void QJsonArray::removeAt(int i); // 刪除i位置的元素
void QJsonArray::removeFirst(); // 刪除頭部
void QJsonArray::removeLast(); // 刪除尾部
QJsonValue QJsonArray::takeAt(int i); // 刪除i位置的原始, 并返回刪除的元素的值
Json 數(shù)組的遍歷,常用的方式有兩種:
可以使用迭代器進(jìn)行遍歷(和使用迭代器遍歷STL容器一樣)可以使用數(shù)組的方式遍
3.3 示例
3.3.1?Json數(shù)組的生成
#include
#include
#include
#include
#include
#include
#include
using namespace std;
//Json數(shù)組生成 [10, "hello itcast", 3.1415, "Qt"]
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//定義一個(gè)Json數(shù)組
QJsonArray array;
//向Json數(shù)組中追加數(shù)據(jù)
array.append(QJsonValue(10));
array.append(QJsonValue("hello itcast"));
array.append(QJsonValue(3.1415));
array.append(QJsonValue("Qt"));
//轉(zhuǎn)為Json文檔
QJsonDocument doc(array);
//轉(zhuǎn)化為字節(jié)數(shù)組
//QByteArray arr = doc.toJson(QJsonDocument::Compact);
QByteArray arr = doc.toJson();
qDebug().noquote() << arr;
qDebug() << arr;
cout << arr.toStdString();
return a.exec();
}
3.3.2?Json數(shù)組生成和解析
#include
#include
#include
#include
#include
#include
#include
using namespace std;
//Json數(shù)組生成 [10, "hello itcast", 3.1415, "Qt"]
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//定義一個(gè)Json數(shù)組
QJsonArray array;
//向Json數(shù)組中追加數(shù)據(jù)
array.append(QJsonValue(10));
array.append(QJsonValue("hello itcast"));
array.append(QJsonValue(3.1415));
array.append(QJsonValue("Qt"));
//轉(zhuǎn)為Json文檔
QJsonDocument doc(array);
//轉(zhuǎn)化為字節(jié)數(shù)組
//QByteArray arr = doc.toJson(QJsonDocument::Compact);
QByteArray arr = doc.toJson();
qDebug().noquote() << arr;
qDebug() << arr;
cout << arr.toStdString();
return a.exec();
}
四、QJsonDocument類(lèi)
QJsonDocument是一個(gè)用于處理JSON格式數(shù)據(jù)的類(lèi),它可以將JSON數(shù)據(jù)轉(zhuǎn)換為QByteArray或QVariant類(lèi)型,并提供了一些方法來(lái)操作JSON數(shù)據(jù);
它封裝了一個(gè)完整的JSON文檔,并且可以從?UTF?8?編碼的基于文本的表示以及Qt自己的二進(jìn)制格式讀取和寫(xiě)入該文檔。
QJsonObject?和?QJsonArray?這兩個(gè)對(duì)象中的數(shù)據(jù)是不能直接轉(zhuǎn)換為字符串類(lèi)型的,如果要進(jìn)行數(shù)據(jù)傳輸或者數(shù)據(jù)的持久化,操作的都是字符串類(lèi)型而不是?QJsonObject?或者?QJsonArray?類(lèi)型,我們需要通過(guò)一個(gè)Json文檔類(lèi)進(jìn)行二者之間的轉(zhuǎn)換。
4.1 常用API
// QJsonDocument常用api
1. fromJson(const QByteArray &json):將 JSON 格式的字節(jié)數(shù)組轉(zhuǎn)換為 QJsonDocument。
2. toJson():將 QJsonDocument 轉(zhuǎn)換為 JSON 格式的字符串。
3. object():返回 QJsonDocument 中的 JSON 對(duì)象。
4. array():返回 QJsonDocument 中的 JSON 數(shù)組。
5. isEmpty():判斷 QJsonDocument 是否為空。
6. isNull():判斷 QJsonDocument 是否為 null。
7. isObject():判斷 QJsonDocument 是否為 JSON 對(duì)象。
8. isArray():判斷 QJsonDocument 是否為 JSON 數(shù)組。
9. isString():判斷 QJsonDocument 是否為 JSON 字符串。
10. isBool():判斷 QJsonDocument 是否為 JSON 布爾值。
11. isDouble():判斷 QJsonDocument 是否為 JSON 數(shù)值。
12. isUndefined():判斷 QJsonDocument 是否為 JSON 未定義值。
13. fromJson(const QString &json):將 JSON 格式的字符串轉(zhuǎn)換為 QJsonDocument。
14. fromVariant(const QVariant &variant):將 QVariant 轉(zhuǎn)換為 QJsonDocument。
15. toVariant():將 QJsonDocument 轉(zhuǎn)換為 QVariant。
4.2 應(yīng)用示例
下面依次介紹一下這兩個(gè)轉(zhuǎn)換流程應(yīng)該如何操作:
QJsonObject?或者?QJsonArray?===>?字符串
創(chuàng)建?QJsonDocument?對(duì)象 QJsonDocument::QJsonDocument(const QJsonObject &object);
QJsonDocument::QJsonDocument(const QJsonArray &array);
可以看出,通過(guò)構(gòu)造函數(shù)就可以將實(shí)例化之后的 **QJsonObject 或者 QJsonArray **轉(zhuǎn)換為QJsonDocument?對(duì)象了。 將文件對(duì)象中的數(shù)據(jù)進(jìn)行序列化 // 二進(jìn)制格式的json字符串
QByteArray QJsonDocument::toBinaryData() const;
// 文本格式
QByteArray QJsonDocument::toJson(JsonFormat format = Indented) const;
通過(guò)調(diào)用?toxxx()?方法就可以得到文本格式或者二進(jìn)制格式的 Json 字符串了。 使用得到的字符串進(jìn)行數(shù)據(jù)傳輸, 或者磁盤(pán)文件持久化 字符串?===>?QJsonObject?或者?QJsonArray 一般情況下,通過(guò)網(wǎng)絡(luò)通信或者讀磁盤(pán)文件就會(huì)得到一個(gè)Json格式的字符串,如果想要得到相關(guān)的原始數(shù)據(jù)就需要對(duì)字符串中的數(shù)據(jù)進(jìn)行解析,具體解析流程如下:
將得到的 Json 格式字符串通過(guò)?QJsonDocument?類(lèi)的靜態(tài)函數(shù)轉(zhuǎn)換為?QJsonDocument?類(lèi)對(duì)象 [static] QJsonDocument QJsonDocument::fromBinaryData(const QByteArray &data, DataValidation validation = Validate);
// 參數(shù)文件格式的json字符串
[static] QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error = Q_NULLPTR);
將文檔對(duì)象轉(zhuǎn)換為json數(shù)組/對(duì)象 // 判斷文檔對(duì)象中存儲(chǔ)的數(shù)據(jù)是不是數(shù)組
bool QJsonDocument::isArray() const;
// 判斷文檔對(duì)象中存儲(chǔ)的數(shù)據(jù)是不是json對(duì)象
bool QJsonDocument::isObject() const
// 文檔對(duì)象中的數(shù)據(jù)轉(zhuǎn)換為json對(duì)象
QJsonObject QJsonDocument::object() const;
// 文檔對(duì)象中的數(shù)據(jù)轉(zhuǎn)換為json數(shù)組
QJsonArray QJsonDocument::array() const;
通過(guò)調(diào)用QJsonArray , QJsonObject 類(lèi)提供的 API 讀出存儲(chǔ)在對(duì)象中的數(shù)據(jù)。 關(guān)于Qt中Json數(shù)據(jù)對(duì)象以及字符串之間的轉(zhuǎn)換的操作流程是固定的,我們?cè)诰幋a過(guò)程中只需要按照上述模板處理即可,相關(guān)的操作是沒(méi)有太多的技術(shù)含量可言的。
五、其他示例
5.1 Write to File
void writeJson()
{
QJsonObject obj;
obj.insert("Name", "Ace");
obj.insert("Sex", "man");
obj.insert("Age", 20);
QJsonObject subObj;
subObj.insert("Father", "Gol·D·Roger");
subObj.insert("Mother", "Portgas·D·Rouge");
QJsonArray array;
array.append("Sabo");
array.append("Monkey D. Luffy");
subObj.insert("Brother", array);
obj.insert("Family", subObj);
obj.insert("IsAlive", false);
obj.insert("Comment", "yyds");
QJsonDocument doc(obj);
QByteArray json = doc.toJson();
QFile file("d:\\ace.json");
file.open(QFile::WriteOnly);
file.write(json);
file.close();
}
5.2 Read from File
void MainWindow::readJson()
{
QFile file("d:\\ace.json");
file.open(QFile::ReadOnly);
QByteArray json = file.readAll();
file.close();
QJsonDocument doc = QJsonDocument::fromJson(json);
if(doc.isObject())
{
QJsonObject obj = doc.object();
QStringList keys = obj.keys();
for(int i=0; i { QString key = keys.at(i); QJsonValue value = obj.value(key); if(value.isBool()) { qDebug() << key << ":" << value.toBool(); } if(value.isString()) { qDebug() << key << ":" << value.toString(); } if(value.isDouble()) { qDebug() << key << ":" << value.toInt(); } if(value.isObject()) { qDebug()<< key << ":"; // 直接處理內(nèi)部鍵值對(duì), 不再進(jìn)行類(lèi)型判斷的演示 QJsonObject subObj = value.toObject(); QStringList ls = subObj.keys(); for(int i=0; i { QJsonValue subVal = subObj.value(ls.at(i)); if(subVal.isString()) { qDebug() << " " << ls.at(i) << ":" << subVal.toString(); } if(subVal.isArray()) { QJsonArray array = subVal.toArray(); qDebug() << " " << ls.at(i) << ":"; for(int j=0; j { // 因?yàn)橹罃?shù)組內(nèi)部全部為字符串, 不再對(duì)元素類(lèi)型進(jìn)行判斷 qDebug() << " " << array[j].toString(); } } } } } } } 一般情況下,對(duì)于Json字符串的解析函數(shù)都是有針對(duì)性的,因?yàn)樾枨蟛煌O(shè)計(jì)的Json格式就會(huì)有所不同,所以不要試圖寫(xiě)出一個(gè)通用的Json解析函數(shù),這樣只會(huì)使函數(shù)變得臃腫而且不易于維護(hù),每個(gè)Json格式對(duì)應(yīng)一個(gè)相應(yīng)的解析函數(shù)即可。 5.3 示例3 #include #include #include #include #include int main() { // 創(chuàng)建一個(gè)JSON對(duì)象 QJsonObject object; object.insert("name", "John"); object.insert("age", 30); // 將JSON對(duì)象轉(zhuǎn)換為QJsonDocument QJsonDocument document(object); // 將QJsonDocument轉(zhuǎn)換為QByteArray QByteArray data = document.toJson(); // 輸出QByteArray qDebug() << data; // 將QByteArray轉(zhuǎn)換為QJsonDocument QJsonDocument newDocument = QJsonDocument::fromJson(data); // 將QJsonDocument轉(zhuǎn)換為JSON對(duì)象 QJsonObject newObject = newDocument.object(); // 獲取JSON對(duì)象中的值 QString name = newObject.value("name").toString(); int age = newObject.value("age").toInt(); // 輸出值 qDebug() << "Name: " << name; qDebug() << "Age: " << age; return 0; } 該程序?qū)?chuàng)建一個(gè)JSON對(duì)象,將其轉(zhuǎn)換為QJsonDocument,然后將其轉(zhuǎn)換為QByteArray。然后,它將QByteArray轉(zhuǎn)換回QJsonDocument,然后將其轉(zhuǎn)換為JSON對(duì)象。最后,它將從JSON對(duì)象中獲取值并將其輸出。 參考文章: 【庫(kù)函數(shù)】Qt中Json的操作 - RioTian - 博客園 (cnblogs.com) 【Qt】Qt中QJsonValue類(lèi)_qt qjsonvalue-CSDN博客 【Qt】Qt中QJsonArray類(lèi)-CSDN博客 QJsonValue Class | Qt Core 6.8.0 柚子快報(bào)邀請(qǐng)碼778899分享:QT中Json類(lèi)應(yīng)用 推薦文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀(guān)點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。