Qt:QJsonValue

0、说明

 QJsonValue类用于操作JSON中的各种数据。

JSON是用于存储结构化数据的格式,JSON中的数据可以是六种类型:

基本类型

存储类型

bool QJsonValue::Bool
double QJsonValue::Double
string QJsonValue::String
array  QJsonValue::Array
object  QJsonValue::Object
null  QJsonValue::Null

这六种类型就能代表任何程序中的类型了。除此之外,QJsonValue还有一个特殊的标签用于指代未定义变量,通过isUndefined()查看这个标签

值的类型通过type()查看,或者直接用isBool()、isString()、...进行判断。同时,JSON中的值通过toBool()、toString()、...转化为程序中的类型

数值类型是内部严格对应的,不同于QVariant,QJsonValue并不会做隐含的强制转型。这意味着转换一个不存在于Value的类型会返回一个默认类型的Value。

1、模块和载入项

Header #include<QJsonValue>
qmake QT += core 
Since Qt 5.0

2、构造

QJsonValue(QJsonValue other)
QJsonValue(QJsonObject o)
QJsonValue(QJsonArray a)

QJsonValue(var s)

var可以是char、QString、qint64、double、bool

3、静态方法

QJsonValue fromVariant(QVariant variant)  

4、实例方法

返回值类型

方法

说明

QJsonValue &

bool

bool

QJsonValue

QJsonValue

operator=(QJsonValue other)

operator!=(QJsonValue other)

operator==(QJsonValue other)

operator[](QString key)

operator[](int i

赋值

判断是否不等

判断是否相等

可以用 [i]的方式提取元素

bool

isArray()

isBool()

isDouble()

isNull()

isObject()

isString()

isUndefined()

类型判断
void swap(QJsonValue &other) 交换两个QJsonValue的值
QJsonArray

toArray(QJsonArray defaultValue)

toArray() 

QJsonValue转换为QJsonArray

bool

double

int

QJsonObject

QJsonObject

QString

QString

QVariant

toBool(bool defaultValue = false)

toDouble(double defaultValue = 0)

toInt(int defaultValue = 0)

toObject(QJsonObject defaultValue)

toObject()

toString()

toString(QString defaultValue)

toVariant()

QJsonValue转换为指定类型
QJsonValue::Type type() QJsonValue中数据的类型

5、QJsonValue、QJsonArray、QJsonObject、QJsonDocument之间的关联

Qt:QJsonDocument以及与QJsonArray、QJsonObject、QJsonValue的关联 - ShineLe - 博客园

posted @ 2021-07-02 14:37  ShineLe  阅读(3214)  评论(0编辑  收藏  举报