您的位置:首页 > 汽车 > 时评 > 西安旅游攻略3天花费_网页开发制作教程_站长平台官网_内容营销的4个主要方式

西安旅游攻略3天花费_网页开发制作教程_站长平台官网_内容营销的4个主要方式

2025/3/10 9:22:59 来源:https://blog.csdn.net/weixin_51795597/article/details/146027641  浏览:    关键词:西安旅游攻略3天花费_网页开发制作教程_站长平台官网_内容营销的4个主要方式
西安旅游攻略3天花费_网页开发制作教程_站长平台官网_内容营销的4个主要方式

一、概述

1、Json是一种轻量级的数据交换格式,它是独立与语言的,和语言没有任何关系,也就是说在任何一种语言里面都可以使用Json,这种数据格式用来组织数据。

2、Json中主要有两种数据格式:Json数组和Json对象,并且这两种格式可以交叉嵌套使用。

二、Json类

1、QJsonDocument

它封装了一个完整的JSON文档,并且可以从UTF-8编码的基于文本的表示,以及Qt自己的二进制格式读取和写入该文档。

2、QJsonArray

Json数组是一个值列表,可以通过从数组中插入和删除QJsonValue来操作该列表 

3、QJsonObject

Json对象是键值对的列表,其中键是唯一的字符串,值由QJsonValue表示  

4、QJsonValue

该类封装了JSON支持的数据类型 

三、QJsonDocument介绍

        QJsonObject和QJsonArray这两个对象的数据是不能直接转换为字符串类型的,如果要进行数据传输或者数据的持久化存储,操作的都是字符串而不是QJsonObject和QJsonArray类型,我们需要通过一个Json文档进行二者之间的转换。

1、QJsonObject 、QJsonArray  ===>   字符串 

(1)创建QJsonDocument对象

//创建QJsonDocument对象
QJsonDocument::QJsonDocument(const QJsonObject &object);
QJsonDocument::QJsonDocument(const QJsonArray &array);

(2) 将文件对象中的数据序列化

//二进制格式的json字符串
QByteArray QJsonDocument::toBinaryData() const;
//文本格式
QByteArray QJsonDocument::toJson(JsonFormat format = Indented) const;

(3)使用得到的字符串进行数据传输,或者磁盘文件持久化 

2、 字符串   ===>   QJsonObject 、QJsonArray

(1)将得到的Json格式的字符串通过QJsonDocument类的静态函数进行转换为QJsonDocument类对象

static QJsonDocument fromBinaryData(const QByteArray &data, DataValidation validation  = Validate);
//参数文件格式的Json字符串
static QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error = nullptr);

(2) 将文档对象转换为json数组/对象

//判断文档对象中存储的数据是不是数组
bool QJsonDocument::isArray() const;
//判断文档对象中存储的数据是不是对象
bool QJsonDocument::isObject() const;//文档对象中的数据格式转换为json对象
QJsonDocument QJsonDocument::object() const;
//文档对象中数据格式转换为json数组
QJsonDocument QJsonDocument::array() const;

四、QJsonValue介绍

1、在Qt中QJsonValue可以封装的基础数据类型有六种(和Json支持的类型一致)

(1)布尔类型

QJsonValue::Bool

(2)浮点类型(包括整形)

QJsonValue::Double 

(3)字符串类型

QJsonValue::String 

(4) Json数组类型

 QJsonValue::Array

(5) Json对象类型

 QJsonObject::Object

(6)空值类型 

QJsonValue::Null

 2、上面的类型可以通过QJsonValue的构造被封装成一个类对象

 //Json数组QJsonValue(const QJsonArray &a);//Json对象QJsonValue(const QJsonObject &o);//布尔类型QJsonValue(bool b);//浮点型、整形QJsonValue(double n);QJsonValue(int n);QJsonValue(qint64 v);//字符串QJsonValue(const QString &s);QJsonValue(QLatin1String s);//空值类型QJsonValue(Type = Null);

3、QJsonValue类型判断函数

 //是否是空值类型inline bool isNull() const { return type() == Null; }//是否是布尔类型inline bool isBool() const { return type() == Bool; }//是否是浮点类型(整形也是通过这个判断)inline bool isDouble() const { return type() == Double; }//是否是字符串类型inline bool isString() const { return type() == String; }//是否是Json数组inline bool isArray() const { return type() == Array; }//是否是Json对象inline bool isObject() const { return type() == Object; }//是否是未定义类型inline bool isUndefined() const { return type() == Undefined; }

4、QJsonValue类型转换函数

//转换为布尔类型
bool toBool(bool defaultValue = false) const;
//转换为整型
int toInt(int defaultValue = 0) const;
//转换为浮点型
double toDouble(double defaultValue = 0) const;
//转换为字符串类型
QString toString() const;
QString toString(const QString &defaultValue) const;
//转换为Json数组
QJsonArray toArray() const;
QJsonArray toArray(const QJsonArray &defaultValue) const;
//转换为Json对象
QJsonObject toObject() const;
QJsonObject toObject(const QJsonObject &defaultValue) const;

五、QJsonObject介绍

        以键值对的形式存储数据,键是QString类型,值是QJsonValue类型。提供了插入、删除和修改键值对的方法。

1、创建空的Json对象

QJsonObject();

2、将键值对添加到空对象中 

  iterator insert(const QString &key, const QJsonValue &value);  

 3、获取对象中键值对个数

   int size() const;
   inline int count() const { return size(); }
   inline int length() const { return size(); }

4、通过key得到value

 QJsonValue value(QStringView key) const;
 QJsonValue value(QLatin1String key) const;
 QJsonValue operator[] (QStringView key) const { return value(key); }
 QJsonValue operator[] (QLatin1String key) const { return value(key); } 

5、删除键值对

void remove(const QString &key);

QJsonValue take(const QString &key); 

六、QJsonArray介绍 

        QJsonArray是Qt框架中的一个类,用于表示JSON数组。JSON数组是一个有序的值列表。其中的值可以是各种类型。

1、创建空的Json数组

 QJsonArray();

2、添加数据

 void prepend(const QJsonValue &value);
 void append(const QJsonValue &value); 

 void insert(int i, const QJsonValue &value);

 void push_back(const QJsonValue &t) { append(t); }
 void push_front(const QJsonValue &t) { prepend(t); }

3、移除数据

 void removeAt(int i);
 QJsonValue takeAt(int i);
 void removeFirst();
 void removeLast();

 void pop_front();
 void pop_back();

4、获取元素

 QJsonValue at(int i) const;
 QJsonValue first() const;
 QJsonValue last() const; 

 QJsonValue operator[](int i) const;

5、获取数组中元素个数

  int size() const;
  int count() const;

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com