Qt-Json相关类
QJsonDocument
QJsonDocument
类常用于读和写 JSON
文档,这里的JSON文档可以是 二进制文档也可以是 UTF-8文档。
QJsonObject
QJsonObject
类封装了一个JSON对象。JSON 对象是一个键值对 <key/value>,key
是独一无二的字符串,value
由一个 QJsonValue
表示。
一个 QJsonObject
可以和一个 QVariantMap
相互转换。可以使用 size()
来查询键值对的梳理,通过 insert()
插入键值对,remove()
删除指定的 key。
注意:由于底层数据结构是 map
,QJsonObject并不能保证顺序。
QJsonArray
QJsonArray
类封装了一个JSON数组。JSON 数组是值的列表,可以和一个 QVariantList
相互转换。可以使用 size()
来查询大小,通过 insert()
在指定索引处插入值,removeAt()
来删除指定索引的值。
注意:QJsonArray 是有序的,这种有序包括读取时有序以及写回时有序。
QJsonValue
QJsonValue
类将一个值封装在JSON中。该值可以是如下6中基本类型:
-
bool
QJsonValue::Bool
-
double
QJsonValue::Double
-
string
QJsonValue::String
-
array
QJsonValue::Array
-
object
QJsonValue::Object
-
null
QJsonValue::Null
一个值可以由任何上述数据类型表示。此外,QJsonValue
有一个特殊的标记来表示未定义的值,可以使用 isUndefined()
查询。
值的类型可以通过 type()
或 isBool()
、isString()
等访问函数查询。同样地,值可以通过 toBool()
、toString()
等函数转化成相应的存储类型。
注意:
QJsonValue 中存储的值在内部是强类型的,并且和 QVariant 相反,它不会尝试进行任何的隐式类型转换。
这意味着将 QJsonValue
转换成一个不是它存储的类型,将返回一个该类型的模型构造函数返回的值,所以在取值前对类型进行判断非常有必要。
QJsonParseError
QJsonParseError
类用于在 JSON 解析中报告错误。
构建json对象
json 就两种类型:对象 及 数组,可任意组合。
由于比较简单,这里直接看一个组合的例子:
{
"Array": [
true,
999,
"string"
],
"Key": "Value",
"null": null
}
生成该 JsonObject
的代码如下:
QJsonObject jsObject;
QJsonArray jsArray = {true, 999, QString("string")};
jsObject.insert("Array",QJsonValue(jsArray));
jsObject.insert("Key",QJsonValue("Value"));
// null
jsObject.insert("null",QJsonValue());
生成js文档
QJsonObject jsObject;
QJsonArray jsArray = {true, 999, QString("string")};
jsObject.insert("Array",QJsonValue(jsArray));
jsObject.insert("Key",QJsonValue("Value"));
// null
jsObject.insert("null",QJsonValue());
QJsonDocument jsDoc(jsObject);
QByteArray binBuffer = jsDoc.toBinaryData();
QByteArray jsonBuffer = jsDoc.toJson();
QFile saveFile1("bindata.json");
saveFile1.open(QIODevice::ReadWrite);
saveFile1.write(binBuffer);
saveFile1.close();
QFile saveFile2("jsondata.json");
saveFile2.open(QIODevice::ReadWrite);
saveFile2.write(jsonBuffer);
saveFile2.close();
Binfile
Utf-8file
json 解析
QString mFileinfo = QFileDialog::getOpenFileName(
this,
tr("Open File"),
QCoreApplication::applicationDirPath(),
tr("Json files(*.json)"));
QFile mFile(mFileinfo);
mFile.open(QIODevice::ReadOnly);
QByteArray mFileBuffer = mFile.readAll();
mFile.close();
/*!
Parsing - BinaryData file.
jsondata file must be use QJsonDocument::fromRawData().
*/
QJsonDocument jsonFile = QJsonDocument::fromBinaryData(mFileBuffer);
if(!jsonFile.isNull()){
if(jsonFile.isObject()){
QJsonObject jsonObject = jsonFile.object();
if(jsonObject.contains("Array")){
QJsonArray array = jsonObject.value("Array").toArray();
for(int i = 0; i < array.size(); i++){
QJsonValue arrayValue = array.at(i);
if(arrayValue.isBool()){
qDebug() << "Array" << i << ": " << arrayValue.toBool();
}
if(arrayValue.isDouble()){
qDebug() << "Array" << i << ": " << arrayValue.toInt();
}
if(arrayValue.isString()){
qDebug() << "Array" << i << ": " << arrayValue.toString();
}
}
}
if(jsonObject.contains("Key")){
QJsonValue keyValue = jsonObject.value("Key");
if(keyValue.isString()){
qDebug() << "Key: " << keyValue.toString();
}
}
if(jsonObject.contains("null")){
QJsonValue nullValue = jsonObject.value("null");
if(nullValue.isNull()){
qDebug() << "null: " << "Null";
}
}
}
}
解析结果