一、qjsondocument簡介
qjsondocument是Qt中提供的用於JSON文檔的讀寫和操作的類。它是通過解析JSON數據,將其轉化為一個QVariant類型的值,並通過相關的函數提取和操作JSON數據的類。
qjsondocument所支持的JSON類型如下:
- 對象(Object)
- 數組(Array)
- 字符串(String)
- 整數(Integer)
- 浮點數(Double)
- 布爾值(Boolean)
- 空(Null)
下面是一個簡單的JSON文檔示例:
{ "name": "Lily", "age": 18, "score": { "chinese": 90, "math": 95, "english": 98 }, "married": false, "hobbies": ["reading", "traveling", "swimming"] }
使用qjsondocument讀取該JSON文檔:
QJsonDocument doc = QJsonDocument::fromJson(jsonText.toUtf8()); QJsonObject obj = doc.object(); QString name = obj.value("name").toString(); int age = obj.value("age").toInt(); QJsonObject score = obj.value("score").toObject(); int chineseScore = score.value("chinese").toDouble(); int mathScore = score.value("math").toDouble(); int englishScore = score.value("english").toDouble(); bool married = obj.value("married").toBool(); QJsonArray hobbies = obj.value("hobbies").toArray(); QStringList hobbyList; for(auto hobby : hobbies){ hobbyList.append(hobby.toString()); }
以上代碼即可讀取JSON文檔中的數據,並保存到對應的變量中。
二、qjsondocument的操作
1、創建JSON文檔
我們可以通過QJsonObject、QJsonArray、QJsonValue這三個類,分別來創建一個包含不同JSON類型數據的JSON文檔。
下面是一個使用QJsonObject、QJsonArray、QJsonValue創建JSON文檔的例子:
QJsonObject person; person.insert("name", "John"); person.insert("age", 32); QStringList fruits = {"apple", "banana", "orange"}; QJsonArray fruitArray; for(auto fruit : fruits){ fruitArray.append(fruit); } person.insert("fruits", fruitArray); QJsonDocument doc(person);
通過以上代碼,我們創建了一個JSON文檔,它包含了名為「name」和「age」的兩個鍵值對,以及一個名為「fruits」的鍵值對,它的值是一個QJsonArray類型,包含了3個字符串元素。
2、讀取JSON數據
我們可以使用QJsonObject、QJsonArray、QJsonValue這三個類,來快速讀取JSON文檔中的對應數據。
下面是一個使用QJsonObject、QJsonArray、QJsonValue讀取JSON文檔數據的例子:
QJsonDocument doc = QJsonDocument::fromJson(jsonText.toUtf8()); QJsonObject obj = doc.object(); QString name = obj.value("name").toString(); int age = obj.value("age").toInt(); QJsonObject score = obj.value("score").toObject(); int chineseScore = score.value("chinese").toDouble(); int mathScore = score.value("math").toDouble(); int englishScore = score.value("english").toDouble(); bool married = obj.value("married").toBool(); QJsonArray hobbies = obj.value("hobbies").toArray(); QStringList hobbyList; for(auto hobby : hobbies){ hobbyList.append(hobby.toString()); }
通過以上代碼,我們可以快速讀取JSON文檔中指定鍵值對的值,以及從JSON數組中讀取元素。
3、寫入JSON數據
我們可以通過QJsonObject、QJsonArray、QJsonValue這三個類,來快速寫入JSON數據。
下面是一個使用QJsonObject、QJsonArray、QJsonValue寫入JSON數據的例子:
QJsonObject person; person.insert("name", "John"); person.insert("age", 32); QStringList fruits = {"apple", "banana", "orange"}; QJsonArray fruitArray; for(auto fruit : fruits){ fruitArray.append(fruit); } person.insert("fruits", fruitArray); QJsonDocument doc(person);
通過以上代碼,我們可以快速寫入JSON文檔中指定鍵值對的值,以及將數據寫入到JSON數組中。
三、qjsondocument的使用
下面是一個完整的JSON文檔讀取、處理和寫入的例子:
#include #include #include #include #include int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); //讀取JSON文檔 QString jsonText = "{\"name\": \"Lily\", \"age\": 18, \"score\": {\"chinese\": 90, \"math\": 95, \"english\": 98}, \"married\": false, \"hobbies\": [\"reading\", \"traveling\", \"swimming\"]}"; QJsonDocument doc = QJsonDocument::fromJson(jsonText.toUtf8()); QJsonObject obj = doc.object(); //處理JSON數據 QString name = obj.value("name").toString(); int age = obj.value("age").toInt(); QJsonObject score = obj.value("score").toObject(); int chineseScore = score.value("chinese").toDouble(); int mathScore = score.value("math").toDouble(); int englishScore = score.value("english").toDouble(); bool married = obj.value("married").toBool(); QJsonArray hobbies = obj.value("hobbies").toArray(); QStringList hobbyList; for(auto hobby : hobbies){ hobbyList.append(hobby.toString()); } //寫入JSON數據 QJsonObject newScore; newScore.insert("chinese", chineseScore + 5); newScore.insert("math", mathScore + 10); newScore.insert("english", englishScore + 8); obj.insert("score", newScore); QJsonArray newHobbies; newHobbies.append("painting"); newHobbies.append("dancing"); obj.insert("hobbies", newHobbies); QJsonDocument newDoc(obj); qDebug() << newDoc.toJson(QJsonDocument::Compact); return a.exec(); }
使用以上代碼,我們可以完整地讀取JSON文檔中的數據,對其進行相關操作,並最終將數據寫入到JSON文檔中。在調式窗口中,我們可以看到更新後的JSON文檔內容。
四、QJsonDocument的注意事項
使用QJsonDocument時需要注意以下幾點:
- 在讀取JSON文檔時,一定要保證JSON數據格式正確,否則會引起讀取失敗。
- QJsonDocument在存儲JSON數據時,並不保證JSON數據的格式和順序。因此,對於需要保證JSON數據格式和順序的場合,需要自行編寫相關代碼。
- qjsondocument在讀取JSON數據時,會自動將數字型、邏輯型等數據類型轉化為QVariant類型。因此,在進行數據讀取時,需要根據具體數據類型進行相應的類型轉換,以便進行相關操作。
五、總結
本文主要介紹了qjsondocument的基本用法。我們可以通過qjsondocument很方便地將JSON數據序列化和反序列化,並對其進行相關操作。在使用qjsondocument時,需要注意JSON數據格式的正確性,同時在讀取數據時要進行類型轉換,以便進行相關操作。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/270105.html