【Qt】Qt中QJsonObject类
生活随笔
收集整理的這篇文章主要介紹了
【Qt】Qt中QJsonObject类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
00. 目錄
文章目錄
- 00. 目錄
- 01. QJsonObject類簡介
- 02. 公有類型
- 03. 公有成員方法
- 04. 公有成員方法解析
- 05. 參考示例
- 06. 附錄
01. QJsonObject類簡介
Header: #include <QJsonObject> qmake: QT += core Since: Qt 5.0? QJsonObject類用于封裝JSON對象。JSON對象是包含鍵值對的鏈表,其中鍵是唯一的字符串,其值由QJsonValue對象。
? QJsonObject可以與QVariantMap相互轉換,可以用size()來獲得鍵值對的數目,insert()、remove()分別用來插入和刪除pair。可以用標準C++的迭代器模式(iterator pattern)來迭代其內容。
? QJsonObject是一個隱式共享的類,只要沒有被改變過,QJsonObject會和創建它的document共享數據。
? 可以通過QJsonDocument將QJsonObject和文本格式相互轉換。
02. 公有類型
class const_iterator class iterator typedef ConstIterator typedef Iterator typedef key_type typedef mapped_type typedef size_type03. 公有成員方法
QJsonObject() QJsonObject(std::initializer_list<QPair<QString, QJsonValue> > args) QJsonObject(const QJsonObject &other) QJsonObject(QJsonObject &&other) ~QJsonObject() QJsonObject::iterator begin() QJsonObject::const_iterator begin() const QJsonObject::const_iterator constBegin() const QJsonObject::const_iterator constEnd() const QJsonObject::const_iterator constFind(const QString &key) const QJsonObject::const_iterator constFind(QLatin1String key) const bool contains(const QString &key) const bool contains(QLatin1String key) const int count() const bool empty() const QJsonObject::iterator end() QJsonObject::const_iterator end() const QJsonObject::iterator erase(QJsonObject::iterator it) QJsonObject::iterator find(const QString &key) QJsonObject::iterator find(QLatin1String key) QJsonObject::const_iterator find(const QString &key) const QJsonObject::const_iterator find(QLatin1String key) const QJsonObject::iterator insert(const QString &key, const QJsonValue &value) bool isEmpty() const QStringList keys() const int length() const void remove(const QString &key) int size() const void swap(QJsonObject &other) QJsonValue take(const QString &key) QVariantHash toVariantHash() const QVariantMap toVariantMap() const QJsonValue value(const QString &key) const QJsonValue value(QLatin1String key) const bool operator!=(const QJsonObject &other) const QJsonObject & operator=(const QJsonObject &other) QJsonObject & operator=(QJsonObject &&other) bool operator==(const QJsonObject &other) const QJsonValue operator[](const QString &key) const QJsonValue operator[](QLatin1String key) const QJsonValueRef operator[](const QString &key) QJsonValueRef operator[](QLatin1String key)04. 公有成員方法解析
QJsonObject::QJsonObject(std::initializer_list<QPair<QString, QJsonValue> > args) 使用鍵值對鏈表構建QJsonObject對象QJsonObject::QJsonObject(const QJsonObject &other) 構建QJsonObject對象iterator QJsonObject::begin() const_iterator QJsonObject::begin() const 返回指向JSON對象的第一個元素的STL風格的迭代器const_iterator QJsonObject::constBegin() const 返回指向JSON對象的第一個元素的const STL風格的迭代器const_iterator QJsonObject::constEnd() const 返回SJON對象的最后一個元素后的位置的const STL風格的迭代器const_iterator QJsonObject::constFind(const QString &key) const 返回一個指向鍵值對中鍵為key的元素的const迭代器bool QJsonObject::contains(const QString &key) const 如果JSON對象中包含鍵key,返回trueint QJsonObject::size() const int QJsonObject::count() const 返回JSON對象中鍵值對的數量bool QJsonObject::empty() const bool QJsonObject::isEmpty() const 如果JSON對象為空,返回trueiterator QJsonObject::find(const QString &key) const_iterator QJsonObject::find(const QString &key) const 返回指向JSON對象中鍵為key的鍵值對的迭代器[static] QJsonObject QJsonObject::fromVariantHash(const QVariantHash &hash) 將hash轉換為JSON對象[static] QJsonObject QJsonObject::fromVariantMap(const QVariantMap &map) 將map轉換為JSON對象iterator QJsonObject::insert(const QString &key, const QJsonValue &value) 插入鍵為key,值為value的鍵值對,返回插入鍵值對的迭代器QStringList QJsonObject::keys() const 返回JSON對象的所有鍵的鏈表void QJsonObject::remove(const QString &key) 刪除JSON對象中的keyQJsonValue QJsonObject::take(const QString &key) 刪除JSON對象中的鍵key,返回key對應的QJsonValueQVariantHash QJsonObject::toVariantHash() const 將JSON對象轉換為QVariantHashQVariantMap QJsonObject::toVariantMap() const 將JSON對象轉換為QVariantMapQJsonValue QJsonObject::value(const QString &key) const 返回key對應的QJsonValue值05. 參考示例
簡單的JSON對象
{"Cross Platform": true,"From": 1991,"Name": "Qt" }生成比較簡單,只需要用 QJsonObject 即可。
// 構建 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")) { // 包含指定的 keyQJsonValue value = object.value("Name"); // 獲取指定 key 對應的 valueif (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;}}} }06. 附錄
官方參考文檔
總結
以上是生活随笔為你收集整理的【Qt】Qt中QJsonObject类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Qt】Qt中QJsonArray类
- 下一篇: 【Qt】Qt中QJsonDocument