当前位置:网站首页>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 !!!!
边栏推荐
- Fiddler realizes mobile packet capturing - getting started
- Introduction to Dynamic Planning II (5.647.62)
- 数仓中的维度表与事实表
- fastjson List转JSONArray以及JSONArray转List「建议收藏」
- 2279. Maximum number of backpacks filled with stones
- Song of cactus - throwing stones to ask the way (3)
- 睿智的目标检测23——Pytorch搭建SSD目标检测平台
- Comment réaliser un graphique Nebula d'importation CSV hors ligne de niveau milliard
- lseek 出错
- Deux séquences ergodiques connues pour construire des arbres binaires
猜你喜欢

华为云服务器安装mysqlb for mysqld.service failed because the control process exited with error code.See “sys

基于 Nebula Graph 构建百亿关系知识图谱实践

解决** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the defau

Crawl the information of national colleges and universities in 1 minute and make it into a large screen for visualization!

图数据库|Nebula Graph v3.1.0 性能报告

爱可可AI前沿推介(7.2)

win10系统升级一段时间后,内存占用过高

智联招聘的基于 Nebula Graph 的推荐实践分享

隐藏在 Nebula Graph 背后的星辰大海

【Experience Cloud】如何在VsCode中取得Experience Cloud的MetaData
随机推荐
HMS core machine learning service helps zaful users to shop conveniently
Two traversal sequences are known to construct binary trees
/Bin/ld: cannot find -lcrypto
蚂蚁集团大规模图计算系统TuGraph通过国家级评测
Floyed "suggestions collection"
Lseek error
中科大脑知识图谱平台建设及业务实践
Finally, I understand the event loop, synchronous / asynchronous, micro task / macro task, and operation mechanism in JS (with test questions attached)
Pyobject to char* (string)
华为云服务器安装mysqlb for mysqld.service failed because the control process exited with error code.See “sys
Locate: cannot execute stat() `/var/lib/mlocate/mlocate Db ': there is no such file or directory
解决BASE64Encoder报错的问题
处理gzip: stdin: not in gzip formattar: Child returned status 1tar: Error is not recoverable: exitin
Teach you how to build virtual machines locally and deploy microservices
6091. 划分数组使最大差为 K
Fastjson list to jsonarray and jsonarray to list "suggested collections"
Pyinstaller打包exe附带图片的方法
数仓中的维度表与事实表
Experiment collection of University Course "Fundamentals of circuit analysis". Experiment 5 - Research on equivalent circuit of linear active two terminal network
Xpt2046 four wire resistive touch screen