当前位置:网站首页>Various entanglements between qvariant and Jason -- QT
Various entanglements between qvariant and Jason -- QT
2022-07-02 15:55:00 【Less than ten years】
Preface
Although I knew QVariant Some of the features of , But not with Json All kinds of mutual transformation . I used to analyze Json after , Is to convert it into my customized structure , Convenient to see , It's easy to call , It is troublesome to parse and convert ; Later, I saw my colleagues directly Json Convert to QVariant( or QVariantMap QVariantList), Then call directly , I think it's good , But there are some confused places in the middle , So write it down here .
All kinds of mutual transformation
First A word of warning , This is also a mistake I often make : Don't think about one step ,QVariant What type of deposit , Just transfer it out of what type !!!
Json To QVariant
QJsonObject To QVariant,QVariant The constructor of contains these types .
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);
Of course ,QJsonArray To QVariant It's the same
QVariant var2(arr);
also ,QJsonDocument To QVariant
QVariant var3(QJsonDocument(obj));
Generally, for the convenience of local call , Is to directly convert it into QVariantMap、QVariantHash or QVariantList
QVariantList varList=arr.toVariantList();
QVariantMap varMap=obj.toVariantMap();
QVariantHash varHash=obj.toVariantHash();
When you use it , Call directly
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 To Json
This only needs direct conversion , Premise is QVariant Originally, the type of deposit is Json
const QJsonObject& _obj=var.toJsonObject();
If deposited QVariant The type is QVariantMap, Then one more step is needed
QVariant var4(obj.toVariantMap());
const QJsonObject& __obj=QJsonObject::fromVariantMap(var4.toMap());
qDebug()<<__obj.value("name1").toString();
QVariant To QByteArray
Look at the help documentation , It can be seen that QVariant There are ways to directly convert into QByteArray, Just convert it directly into QByteArray, So what you got QByteArray It's probably empty ~
I still emphasize depositing QVariant What is the , You have to convert it directly to why , As for the other types , Continue to convert .
If deposited QVariant Yes. QJsonObject And so on
qDebug()<<QJsonDocument(var.toJsonObject()).toJson(QJsonDocument::Compact);
If the deposit is QVariant Yes. QVariantMap And so on
qDebug()<<QJsonDocument::fromVariant(var4).toJson();
If the deposit is QByteArray, So what to say , Of course, direct conversion , Of course , The result is not empty , Unless you store an empty byte array
QJsonDocument doc(obj);
QVariant var5(doc.toJson(QJsonDocument::Compact));
qDebug()<<var5.toByteArray();
Conclusion
QVariant What is deposited in , Extracted , The type must correspond to !!!!
边栏推荐
- Flink real-time data warehouse (IX): incremental synchronization of data in MySQL
- The sea of stars hidden behind the nebula graph
- 又是一年毕业季
- 已知兩種遍曆序列構造二叉樹
- 2279. Maximum number of backpacks filled with stones
- 数据湖(十一):Iceberg表数据组织与查询
- /Bin/ld: cannot find -lpam
- Locate: cannot execute stat() `/var/lib/mlocate/mlocate Db ': there is no such file or directory
- 使用FFmpeg命令行进行UDP、RTP推流(H264、TS),ffplay接收
- Moveit 避障路径规划 demo
猜你喜欢
《大学“电路分析基础”课程实验合集.实验四》丨线性电路特性的研究
【Salesforce】如何确认你的Salesforce版本?
Comparison between rstan Bayesian regression model and standard linear regression model of R language MCMC
隐藏在 Nebula Graph 背后的星辰大海
数仓中的维度表与事实表
动态规划入门一,队列的bfs(70.121.279.200)
PostgresSQL 流复制 主备切换 主库无读写宕机场景
Idea jar package conflict troubleshooting
XPT2046 四线电阻式触摸屏
已知两种遍历序列构造二叉树
随机推荐
数组和链表的区别浅析
Usage of group by
How to import a billion level offline CSV into Nepal graph
数据湖(十一):Iceberg表数据组织与查询
将点云坐标转换成世界坐标的demo
目标检测—利用labelimg制作自己的深度学习目标检测数据集
动态规划入门二(5.647.62)
Introduction to Dynamic Planning II (5.647.62)
/bin/ld: 找不到 -lpam
Solve the problem of base64encoder error
Target detection - make your own deep learning target detection data set with labelimg
Huawei ECS installs mysqlb for mysqld service failed because the control process exited with error code. See “sys
Aike AI frontier promotion (7.2)
/Bin/ld: cannot find -lcrypto
Demo of converting point cloud coordinates to world coordinates
(5) Flink's table API and SQL update mode and Kafka connector case
Use ffmpeg command line to push UDP and RTP streams (H264 and TS), and ffplay receives
/bin/ld: 找不到 -lcrypto
数仓中的维度表与事实表
隐藏在 Nebula Graph 背后的星辰大海