QT/qt(16)json文件
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,
同时也易于机器解析和生成。
JSON 采用完全独立于语言的文本格式,
但也使用了类似于 C 语言家族的习惯(包括 C、C++、C#、Java、JavaScript、Perl、Python 等)。
这些特性使得 JSON 成为理想的数据交换语言。
JSON 数据结构主要有两种:
1. 对象(Object): 对象是一个无序的键值对集合。键必须是字符串,值可以是字符串、数字、对象、数组、布尔值或 null。
{
"name": "John",
"age": 30,
"city": "New York"
}
2. 数组(Array): 数组是一个有序的值集合。值可以是字符串、数字、对象、数组、布尔值或 null。
[
"apple",
123,
true,
null,
{
"name": "John",
"age": 30
}
]
JSON 数据类型
JSON 支持以下数据类型:
字符串(String):用双引号括起来的字符序列。
"Hello, World!"
数字(Number):整数或浮点数。
123
布尔值(Boolean):true 或 false。
true
对象(Object):无序的键值对集合。
{
"name": "John",
"age": 30
}
数组(Array):有序的值集合。
[1, 2, 3, 4, 5]
null:表示空值。
null
QJsonDocument 类
QJsonDocument 类是用来表示 JSON 文档的类。
它提供了读取、写入和操作 JSON 数据的功能。
class QJsonDocument {
public:
// 构造函数
QJsonDocument(); // 默认构造函数,创建一个空的 JSON 文档
QJsonDocument(const QJsonDocument &other); // 拷贝构造函数
QJsonDocument(const QJsonObject &object); // 从 QJsonObject 构造 JSON 文档
QJsonDocument(const QJsonArray &array); // 从 QJsonArray 构造 JSON 文档
// 析构函数
~QJsonDocument();
// 公共成员函数
QJsonDocument &operator=(const QJsonDocument &other); // 赋值运算符
void swap(QJsonDocument &other) noexcept; // 交换函数
bool operator==(const QJsonDocument &other) const; // 相等运算符
bool operator!=(const QJsonDocument &other) const; // 不等运算符
bool isEmpty() const; // 检查文档是否为空
bool isArray() const; // 检查文档是否为数组
bool isObject() const; // 检查文档是否为对象
QJsonArray array() const; // 返回文档的数组
QJsonObject object() const; // 返回文档的对象
void setArray(const QJsonArray &array); // 设置文档为数组
void setObject(const QJsonObject &object); // 设置