当前位置:网站首页>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里存入的是什么,取出来的,类型一定要对应上!!!!
边栏推荐
- (Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
- PostgresSQL 流复制 主备切换 主库无读写宕机场景
- 【LeetCode】695-岛屿的最大面积
- Finally, I understand the event loop, synchronous / asynchronous, micro task / macro task, and operation mechanism in JS (with test questions attached)
- 【LeetCode】189-轮转数组
- The outline dimension function application of small motherboard
- 2279. Maximum number of backpacks filled with stones
- 【LeetCode】200-岛屿数量
- 基于 Nebula Graph 构建百亿关系知识图谱实践
- 全是精华的模电专题复习资料:基本放大电路知识点
猜你喜欢

How to import a billion level offline CSV into Nepal graph

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

The sea of stars hidden behind the nebula graph

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

Two traversal sequences are known to construct binary trees

Basic knowledge of cryptography
![[salesforce] how to confirm your salesforce version?](/img/ce/4c844b1b686397faa1b6aa3d57e034.png)
[salesforce] how to confirm your salesforce version?

蚂蚁集团大规模图计算系统TuGraph通过国家级评测

PostgresSQL 流复制 主备切换 主库无读写宕机场景
![[leetcode] 1254 - count the number of closed Islands](/img/84/f888ae0e164951cd9623fb3bf4a984.png)
[leetcode] 1254 - count the number of closed Islands
随机推荐
Experiment collection of University "Fundamentals of circuit analysis". Experiment 7 - Research on sinusoidal steady-state circuit
奥比中光 astra: Could not open “2bc5/[email protected]/6“: Failed to set USB interface
[development environment] install the Chinese language pack for the 2013 version of visual studio community (install test agents 2013 | install visual studio 2013 simplified Chinese)
SQL FOREIGN KEY
Golang MD5 encryption and MD5 salt value encryption
【LeetCode】1905-统计子岛屿
睿智的目标检测23——Pytorch搭建SSD目标检测平台
/Bin/ld: cannot find -llz4
智联招聘的基于 Nebula Graph 的推荐实践分享
Introduction to dynamic planning I, BFS of queue (70.121.279.200)
使用FFmpeg命令行进行UDP、RTP推流(H264、TS),ffplay接收
动态规划入门二(5.647.62)
[leetcode] 189 rotation array
Solve the problem of base64encoder error
如何实现十亿级离线 CSV 导入 Nebula Graph
Experiment collection of University "Fundamentals of circuit analysis". Experiment 4 - Research on linear circuit characteristics
隐藏在 Nebula Graph 背后的星辰大海
[network security] network asset collection
2303. 计算应缴税款总额
Some problems about pytorch extension