当前位置:网站首页>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里存入的是什么,取出来的,类型一定要对应上!!!!
边栏推荐
- 2303. Calculate the total tax payable
- 智联招聘的基于 Nebula Graph 的推荐实践分享
- Soul torture, what is AQS???
- [leetcode] 1140 stone game II
- [salesforce] how to confirm your salesforce version?
- Fastjson list to jsonarray and jsonarray to list "suggested collections"
- [idea] recommend an idea translation plug-in: translation "suggestions collection"
- Xpt2046 four wire resistive touch screen
- XPT2046 四线电阻式触摸屏
- 【LeetCode】977-有序數組的平方
猜你喜欢

Review materials for the special topic of analog electronics with all essence: basic amplification circuit knowledge points

【LeetCode】1905-统计子岛屿

Tree binary search tree

已知两种遍历序列构造二叉树

PostgresSQL 流复制 主备切换 主库无读写宕机场景

《大学“电路分析基础”课程实验合集.实验六》丨典型信号的观察与测量

Basic knowledge of cryptography

【LeetCode】1254-统计封闭岛屿的数量

Experiment collection of University "Fundamentals of circuit analysis". Experiment 4 - Research on linear circuit characteristics
![[leetcode] 1905 statistics sub Island](/img/82/d2f7b829f5beb7f9f1eabe8d101ecb.png)
[leetcode] 1905 statistics sub Island
随机推荐
解决BASE64Encoder报错的问题
/Bin/ld: cannot find -lssl
树-二叉搜索树
2303. 计算应缴税款总额
List of sergeant schools
Comparison between rstan Bayesian regression model and standard linear regression model of R language MCMC
Strings and arrays
Pyinstaller打包exe附带图片的方法
[leetcode] 200 number of islands
SQL FOREIGN KEY
Basic knowledge of cryptography
6090. 极大极小游戏
Astra: could not open "2bc5/ [email protected] /6“: Failed to set USB interface
(Wanzi essence knowledge summary) basic knowledge of shell script programming
2278. Percentage of letters in string
Deux séquences ergodiques connues pour construire des arbres binaires
6095. Strong password checker II
How to import a billion level offline CSV into Nepal graph
Application of visualization technology in Nebula graph
6091. Divide the array so that the maximum difference is K