当前位置:网站首页>QVariant与Json的各种纠葛——Qt
QVariant与Json的各种纠葛——Qt
2022-07-02 12:28:00 【十年之少】
前言
我之前虽了解QVariant的一些性能,但是没有与Json各种的互转。以前我解析Json后,是将其转化为我自定义的结构体,既方便看,又方便调用,就是解析转换的时候麻烦些;后续看到同事直接将Json转换为QVariant(或QVariantMap QVariantList),然后直接调用,觉得也不错,但是中间有一些迷糊的地方,所以在此处记录下来。
各种互转
先提醒一句,这也是我常犯的错误:千万别想着一步到位,QVariant存入的是什么类型,就将其转出什么类型!!!
Json转为QVariant
QJsonObject转为QVariant,QVariant的构造函数是包含这些类型的。
QJsonObject obj;
obj.insert("name1","val1");
obj.insert("name2","val2");
obj.insert("name3","val3");
QJsonArray arr;
for(int i=0;i<5;i++)
arr.append(i);
obj.insert("name4",arr);
QVariant var(obj);当然 ,QJsonArray转为QVariant也是一样的
QVariant var2(arr);还有,QJsonDocument转为QVariant
QVariant var3(QJsonDocument(obj));一般为了方便本地调用,是直接将其转化为QVariantMap、QVariantHash或QVariantList
QVariantList varList=arr.toVariantList();
QVariantMap varMap=obj.toVariantMap();
QVariantHash varHash=obj.toVariantHash();使用的时候,直接调用
qDebug()<<"varName_1="<<varMap.value("name1").toString();
qDebug()<<"varName_2="<<varHash.value("name2").toString();
for(int i=0;i<varList.count();i++){
qDebug()<<varList.at(i).toInt();
}QVariant转为Json
这个只需要直接转换就可以了,前提是QVariant原本存入是类型就是Json
const QJsonObject& _obj=var.toJsonObject();若存入QVariant的类型是QVariantMap,那么就要多一个步骤了
QVariant var4(obj.toVariantMap());
const QJsonObject& __obj=QJsonObject::fromVariantMap(var4.toMap());
qDebug()<<__obj.value("name1").toString();
QVariant转为QByteArray
看帮助文档,可以看出QVariant有方法可以直接转化成QByteArray,就直接将其转换为QByteArray,那么你拿到的QByteArray很可能就是空的~
我还是强调存入QVariant的是什么,你就要直接将其转换为什么,至于其他的类型,再继续转换。
若存入QVariant的是QJsonObject之类
qDebug()<<QJsonDocument(var.toJsonObject()).toJson(QJsonDocument::Compact);
若存入的是QVariant的是QVariantMap之类
qDebug()<<QJsonDocument::fromVariant(var4).toJson();若存入的是QByteArray,那么什么好说的,当然直接转换,当然,出来的结果不是空的,除非你存入的是空的字节数组
QJsonDocument doc(obj);
QVariant var5(doc.toJson(QJsonDocument::Compact));
qDebug()<<var5.toByteArray();
结束语
QVariant里存入的是什么,取出来的,类型一定要对应上!!!!
边栏推荐
- 可视化技术在 Nebula Graph 中的应用
- /bin/ld: 找不到 -lgssapi_krb5
- Astra: could not open "2bc5/ [email protected] /6“: Failed to set USB interface
- SQL modification statement
- [leetcode] 1140 stone game II
- /bin/ld: 找不到 -lxml2
- Thoroughly understand browser strong cache and negotiation cache
- 爱可可AI前沿推介(7.2)
- PostgresSQL 流复制 主备切换 主库无读写宕机场景
- Comparison between rstan Bayesian regression model and standard linear regression model of R language MCMC
猜你喜欢
![[leetcode] 1162 map analysis](/img/9a/d04bde0417d4d5232950a4e260eb91.png)
[leetcode] 1162 map analysis

爱可可AI前沿推介(7.2)

全是精华的模电专题复习资料:基本放大电路知识点

The outline dimension function application of small motherboard

Thoroughly understand browser strong cache and negotiation cache

Experiment collection of University Course "Fundamentals of circuit analysis". Experiment 5 - Research on equivalent circuit of linear active two terminal network

已知兩種遍曆序列構造二叉樹

如何实现十亿级离线 CSV 导入 Nebula Graph
![[salesforce] how to confirm your salesforce version?](/img/ce/4c844b1b686397faa1b6aa3d57e034.png)
[salesforce] how to confirm your salesforce version?

如何實現十億級離線 CSV 導入 Nebula Graph
随机推荐
[leetcode] 417 - Pacific Atlantic current problem
《大学“电路分析基础”课程实验合集.实验六》丨典型信号的观察与测量
Ssh/scp does not prompt all activities are monitored and reported
/Bin/ld: cannot find -lcrypto
[leetcode] 577 reverse word III in string
College entrance examination admission score line crawler
愛可可AI前沿推介(7.2)
动态规划入门二(5.647.62)
How to use percona tool to add fields to MySQL table after interruption
隐藏在 Nebula Graph 背后的星辰大海
/bin/ld: 找不到 -lxml2
Pyinstaller's method of packaging pictures attached to exe
[leetcode] 200 number of islands
Wechat Alipay account system and payment interface business process
2278. 字母在字符串中的百分比
使用FFmpeg命令行进行UDP、RTP推流(H264、TS),ffplay接收
Experiment collection of University "Fundamentals of circuit analysis". Experiment 7 - Research on sinusoidal steady-state circuit
[leetcode] 877 stone game
【LeetCode】977-有序数组的平方
/Bin/ld: cannot find -llz4