蜘蛛爬取网站,网站照片要求,杭州做网站怎么收费,wordpress the7.5Qt中解析JSON文件
在Qt中解析JSON字符串主要有两种方式#xff1a;使用QJsonDocument类或使用QJsonDocument结合QVariant。以下是详细的解析方法#xff1a;
使用QJsonDocument#xff08;推荐#xff09;
这种方式的主要相关类如下#xff1a;
QJsonDocument: QJsonDocum…Qt中解析JSON文件
在Qt中解析JSON字符串主要有两种方式使用QJsonDocument类或使用QJsonDocument结合QVariant。以下是详细的解析方法
使用QJsonDocument推荐
这种方式的主要相关类如下
QJsonDocument: QJsonDocument 是 Qt 中处理 JSON 数据的核心类它提供了在内存中读写 JSON 文档的功能。
主要功能
解析 JSON从 UTF-8 编码的文本创建 JSON 文档生成 JSON将内存中的 JSON 数据转换为字符串或二进制格式数据访问提供对 JSON 对象和数组的访问接口格式转换支持紧凑和缩进两种输出格式
QJsonObjectQJsonObject 是 Qt 中用于表示 JSON 对象的类它存储键值对集合其中键是字符串值可以是各种 JSON 类型。
主要特性
键值对存储存储 QString 作为键QJsonValue 作为值无序集合键的顺序不保证与插入顺序一致隐式共享使用写时复制技术拷贝开销小类型安全提供多种类型检查和转换方法
QJsonValue:
QJsonValue 是 Qt 中表示 JSON 值的类它可以存储 JSON 标准支持的所有数据类型。它是 Qt JSON 系统的核心基础类。
主要特性
多类型存储可以存储 JSON 支持的所有数据类型值语义拷贝和赋值操作是高效的隐式共享类型安全提供严格的类型检查和转换方法空值和未定义值支持 JSON null 和未定义值
支持的数据类型
QJsonValue 可以存储以下类型的值类型描述对应的 Qt 类型NullJSON null 值QJsonValue::NullBool布尔值boolDouble双精度浮点数doubleString字符串QStringArrayJSON 数组QJsonArrayObjectJSON 对象QJsonObjectUndefined未定义值无效值QJsonValue::UndefinedQJsonArray:
QJsonArray 是 Qt 中用于表示 JSON 数组的类它存储有序的 QJsonValue 集合。JSON 数组是值的有序列表可以包含不同类型的元素。
主要特性
有序集合元素保持插入顺序混合类型可以存储不同类型的值隐式共享使用写时复制技术拷贝开销小动态大小支持动态添加和删除元素索引访问支持通过索引随机访问元素
下面我们就用进行简单的实战解析。
一、解析简单Json对象
#include QCoreApplication
#includeQJsonDocument
#includeQJsonObject
#includeQJsonValue
#includeQJsonArrayvoid PraseJson()
{QString strJson R({name:张三,age:30,isStudent:false});QJsonDocument doc QJsonDocument::fromJson(strJson.toUtf8());if(doc.isNull() || !doc.isObject()){qDebug() Json解析失败;return;}QJsonObject obj doc.object();QString strName obj[name].toString();int nAge obj[age].toInt();bool bStudent obj[isStudent].toBool();qDebug() 名字: strName;qDebug() 年龄: nAge;qDebug() 是否学生: bStudent;}int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);// Set up code that uses the Qt event loop here.// Call a.quit() or a.exit() to quit the application.// A not very useful example would be including// #include QTimer// near the top of the file and calling// QTimer::singleShot(5000, a, QCoreApplication::quit);// which quits the application after 5 seconds.// If you do not need a running Qt event loop, remove the call// to a.exec() or use the Non-Qt Plain C Application template.PraseJson();return a.exec();
}
代码运行结果二、解析嵌套Json对象
void PraseJson2()
{QString strJson R({person:{name:李四,address:{city:北京,street:长安街}},hobbies:[读书,音乐,运动]});QJsonDocument doc QJsonDocument::fromJson(strJson.toUtf8());if(doc.isNull() || !doc.isObject()){qDebug() Json解析失败;return;}QJsonObject rootObj doc.object();QJsonObject personObj rootObj[person].toObject();QJsonObject addressObj personObj[address].toObject();QString strName personObj[name].toString();qDebug() 名字: strName;QString strCity addressObj[city].toString();qDebug() 城市 strCity;QString strStreet addressObj[street].toString();qDebug() 街道: strStreet;QJsonArray arr rootObj[hobbies].toArray();qDebug() 爱好:;for (const QJsonValue value: arr){qDebug() value.toString();}}代码运行结果三、解析Json数组
void PraseJson3()
{QString strJson R([{name:王五,age:30},{name:赵六,age:35},{name:张三,age:20}]);QJsonDocument doc QJsonDocument::fromJson(strJson.toUtf8());if(doc.isNull()){qDebug() Json解析失败;return;}if(doc.isArray()){QJsonArray arr doc.array();for(const QJsonValue value:arr){QJsonObject obj value.toObject();qDebug() 名字: obj[name].toString() ,年龄: obj[age].toInt();}}
}代码运行结果四、错误处理
void parseJsonWithErrorHandling() {QString jsonString R({invalid: json}); // 无效的JSONQJsonParseError error;QJsonDocument doc QJsonDocument::fromJson(jsonString.toUtf8(), error);if (error.error ! QJsonParseError::NoError) {qDebug() JSON解析错误: error.errorString();return;}if (doc.isNull()) {qDebug() 文档为空;return;}// 安全地获取值QJsonObject obj doc.object();QString value obj.value(key).toString(默认值); // 如果key不存在返回默认值// 检查值类型if (obj.contains(key) obj[key].isString()) {// 安全处理}
}五、构建Json对象
void createJson() {QJsonObject person;person[name] 张三;person[age] 30;QJsonArray hobbies;hobbies.append(读书);hobbies.append(游泳);hobbies.append(编程);person[hobbies] hobbies;QJsonObject address;address[city] 上海;address[street] 南京路;person[address] address;QJsonDocument doc(person);QString jsonString doc.toJson(QJsonDocument::Indented);qDebug() 生成的JSON:;qDebug() jsonString;
}代码运行结果注意事项
编码问题: 确保JSON字符串使用UTF-8编码错误处理: 总是检查解析是否成功类型安全: 在访问值之前检查其类型内存管理: Qt的JSON类使用隐式共享无需担心深拷贝性能问题
常用方法
QJsonDocument::fromJson() - 从字符串创建文档QJsonObject::value() - 安全获取值可指定默认值QJsonValue::toInt(), toString(), toBool() - 类型转换QJsonObject::contains() - 检查键是否存在QJsonValue::isObject(), isArray(), isString() - 检查值类型
: Qt的JSON类使用隐式共享无需担心深拷贝性能问题