Qt提供了一系列類以供進行Json 文檔的讀寫,分別爲:html
QJsonDocumentJson文檔、QJsonArray數組、QJsonObject對象、QJsonValue值、QJsonParseError錯誤。json
Constant | Value | Description |
---|---|---|
QJsonParseError::NoError |
0 |
No error occurred |
QJsonParseError::UnterminatedObject |
1 |
An object is not correctly terminated with a closing curly bracket |
QJsonParseError::MissingNameSeparator |
2 |
A comma separating different items is missing |
QJsonParseError::UnterminatedArray |
3 |
The array is not correctly terminated with a closing square bracket |
QJsonParseError::MissingValueSeparator |
4 |
A colon separating keys from values inside objects is missing |
QJsonParseError::IllegalValue |
5 |
The value is illegal |
QJsonParseError::TerminationByNumber |
6 |
The input stream ended while parsing a number |
QJsonParseError::IllegalNumber |
7 |
The number is not well formed |
QJsonParseError::IllegalEscapeSequence |
8 |
An illegal escape sequence occurred in the input |
QJsonParseError::IllegalUTF8String |
9 |
An illegal UTF8 sequence occurred in the input |
QJsonParseError::UnterminatedString |
10 |
A string wasn’t terminated with a quote |
QJsonParseError::MissingObject |
11 |
An object was expected but couldn’t be found |
QJsonParseError::DeepNesting |
12 |
The JSON document is too deeply nested for the parser to parse it |
QJsonParseError::DocumentTooLarge |
13 |
The JSON document is too large for the parser to parse it |
QJsonParseError::GarbageAtEnd |
14 |
The parsed document contains additional garbage characters at the end |
QJsonDocument::toJson能夠生成json文檔,具備可選參數,能夠生成緊湊結構和縮進結構:數組
Constant | Value | Description |
---|---|---|
QJsonDocument::Indented |
0 |
Defines human readable output as follows: { "Array": [ true, 999, "string" ], "Key": "Value", "null": null } |
QJsonDocument::Compact |
1 |
Defines a compact output as follows: {"Array":[true,999,"string"],"Key":"Value","null":null} |
除此之外還能夠用toBinaryData、toVariant用於結果輸出app
QJsonDocument除了使用構造函數建立之外,還支持靜態函數建立,主要用於讀取已有文件的內容:curl
QJsonDocument並不會直接操做文件,須要自行利用QFile進行readAll或者Writeide
fromRawData/fromBinaryData並不會返回QJsonParseError錯誤而是直接返回DataValidation枚舉類型,代表讀取的數據是否有效函數
Constant | Value | Description |
---|---|---|
QJsonDocument::Validate |
0 |
Validate the data before using it. This is the default. |
QJsonDocument::BypassValidation |
1 |
Bypasses data validation. Only use if you received the data from a trusted place and know it’s valid, as using of invalid data can crash the application. |
QJsonValue用於存儲全部值,能夠用type判斷其類型,含如下類型post
Constant | Value | Description |
---|---|---|
QJsonValue::Null |
0x0 |
A Null value |
QJsonValue::Bool |
0x1 |
A boolean value. Use toBool() to convert to a bool. |
QJsonValue::Double |
0x2 |
A double. Use toDouble() to convert to a double. |
QJsonValue::String |
0x3 |
A string. Use toString() to convert to a QString. |
QJsonValue::Array |
0x4 |
An array. Use toArray() to convert to a QJsonArray. |
QJsonValue::Object |
0x5 |
An object. Use toObject() to convert to a QJsonObject. |
QJsonValue::Undefined |
0x80 |
The value is undefined. This is usually returned as an error condition, when trying to read an out of bounds value in an array or a non existent key in an object. |
也能夠經過isXXXX用於判斷,並經過toXXXX轉換爲對應類型jsonp
此時使用QJsonDocument::Compact方式寫出,其結果爲:url
「{\」Array\」:[true,999,\」string\」],\」Key\」:\」Value\」,\」null\」:null}」
QDebug會將\n直接輸出成\n而不會換行
結果
根據正常的結構進行判斷便可,對於array須要進行遍歷,支持C++的for(:)方式遍歷
fromJson、fromBinaryData、fromRawData、fromVariant這幾個靜態函數都不會直接返回成功與否,而是在參數中實現解析結果判斷,正式使用時務必進行判斷,避免後續代碼均出錯