QT json文件

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); // 设置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可能只会写BUG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值