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这几个静态函数都不会直接返回成功与否,而是在参数中实现解析结果判断,正式使用时务必进行判断,避免后续代码均出错