QT中使用json還是比較方便的,下面用例子直接說明
舉例子之前首先推薦一個在線解析json格式的網站,具體格式用法如下圖所示:
之后根據這個格式進行json數據解析。
QT使用json需要包含的頭文件
#include<qjsondocument.h>
#include<qjsonarray.h>
#include<qjsonobject.h>
#include<qjsonvalue.h>
先看一段簡單的生成QJSON數據的方法吧:
//簡單的QTJson數據
QJsonObject simp_ayjson;
simp_ayjson.insert("name", "LLH");
simp_ayjson.insert("age", 20);
simp_ayjson.insert("myAccounts", QString::fromLocal8Bit("123你好"));
QJsonDocument document;
document.setObject(simp_ayjson);
QByteArray simpbyte_array = document.toJson(QJsonDocument::Compact);
QString simpjson_str(simpbyte_array);
qDebug() << QString::fromLocal8Bit("簡單的QTJson數據:") << simpjson_str;
輸出結果:
"簡單的QTJson數據:" "{\"age\":20,\"myAccounts\":\"123你好\",\"name\":\"LLH\"}"
解析方法是:
//簡單的QT解析JSON
QJsonParseError simp_json_error;
QString name, myAccount;
int age;
//QTJSON所有的元素
QJsonDocument simp_parse_doucment = QJsonDocument::fromJson(simpjson_str.toUtf8(), &simp_json_error);
//檢查json是否有錯誤
if (simp_json_error.error == QJsonParseError::NoError)
{
if (simp_parse_doucment.isObject())
{
//開始解析json對象
QJsonObject obj = simp_parse_doucment.object();
//如果包含name
if (obj.contains("name"))
{
//的到name
QJsonValue name_value = obj.take("name");
if (name_value.isString())
{
//轉換name
name = name_value.toVariant().toString();
}
}
if (obj.contains("myAccounts"))
{
QJsonValue myAccount_value = obj.take("myAccounts");
if (myAccount_value.isString())
{
myAccount = myAccount_value.toString();
}
}
if (obj.contains("age"))
{
QJsonValue age_value = obj.take("age");
if (age_value.isDouble())
{
age = age_value.toVariant().toInt();
}
}
}
}
qDebug() << QString::fromLocal8Bit("簡單的QT解析出來的數據:") << name << "," << age << "," << myAccount;
解析結果:
"簡單的QT解析出來的數據:" "LLH" , 20 , "123你好"
其實這種json能滿足一般的要求但是有些特殊數據要求json里面帶有json數組就需要復雜一點的json了,來吧讓我們繼續。
復雜的json生成代碼:
//復雜的QTJson數據
QJsonArray arrays;
for (int i = 0; i < 5;i++)
{
arrays.insert(i, QString::fromLocal8Bit("你好:%1").arg(i));
}
QJsonObject complex_json;
complex_json.insert("name", "LLHlllll");
complex_json.insert("age", 201);
complex_json.insert("myAccounts", QString::fromLocal8Bit("123你好llll"));
complex_json.insert("arrays", arrays);
QJsonDocument complex_document;
complex_document.setObject(complex_json);
QByteArray complex_byte_array = complex_document.toJson(QJsonDocument::Compact);
QString complex_json_str(complex_byte_array);
qDebug() <<"\n"<< QString::fromLocal8Bit("復雜的QTJson數據:") << complex_json_str;
輸出結果:
"復雜的QTJson數據:" "{\"age\":201,\"arrays\":[\"你好:0\",\"你好:1\",\"你好:2\",\"你好:3\",\"你好:4\"],\"myAccounts\":\"123你好llll\",\"name\":\"LLHlllll\"}"
那么讓我們來看看復雜的json如何解析吧:
//復雜的QT數據解析
QJsonParseError complex_json_error;
QJsonDocument complex_parse_doucment = QJsonDocument::fromJson(complex_json_str.toUtf8(), &complex_json_error);
if (complex_json_error.error == QJsonParseError::NoError)
{
if (complex_parse_doucment.isObject())
{
//開始解析json對象
QJsonObject jsonObject = complex_parse_doucment.object();
if (jsonObject.contains("name"))
{
QJsonValue groupName_value = jsonObject.take("name");
if (groupName_value.isString())
{
name = groupName_value.toString();
}
}
if (jsonObject.contains("myAccounts"))
{
QJsonValue myAccount_value = jsonObject.take("myAccounts");
if (myAccount_value.isString())
{
myAccount = myAccount_value.toVariant().toString();;
}
}
if (jsonObject.contains("age"))
{
QJsonValue groupIndex_value = jsonObject.take("age");
if (groupIndex_value.isDouble())
{
age = groupIndex_value.toVariant().toInt();;
}
}
if (jsonObject.contains("arrays"))
{
QJsonValue arrays_value = jsonObject.take("arrays");
if (arrays_value.isArray())//判斷他是不是json數組
{
QJsonArray heeloArray = arrays_value.toArray();;
for (int i = 0; i < heeloArray.size(); i++){
QJsonValue helloArrayValue = heeloArray.at(i);
if (helloArrayValue.isString())
{
qDebug() <<"\n"<< QString::fromLocal8Bit("輸出復雜的json:") << helloArrayValue.toString();
}
}
}
}
}
}
可以看到復雜的json解析代碼有點長,原因可能是QT喜歡w3c就是docment元素的解析風格,也就是docment樹解析,把數據加載成一顆樹,然后在進行解析,好處是速度快,缺點是需要大量內存,代碼雖然長點但是好理解,因為每一步都他的解析方式。
解析結果:
"輸出復雜的json:" "你好:0"
"輸出復雜的json:" "你好:1"
"輸出復雜的json:" "你好:2"
"輸出復雜的json:" "你好:3"
"輸出復雜的json:" "你好:4"