当前位置:网站首页>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 !!!!
边栏推荐
- win10系统升级一段时间后,内存占用过高
- 2279. Maximum number of backpacks filled with stones
- Make p12 certificate [easy to understand]
- /Bin/ld: cannot find -lgssapi_ krb5
- 目标检测—利用labelimg制作自己的深度学习目标检测数据集
- 2303. Calculate the total tax payable
- Locate: cannot execute stat() `/var/lib/mlocate/mlocate Db ': there is no such file or directory
- Golang MD5 encryption and MD5 salt value encryption
- matlab中wavedec2,说说wavedec2函数[通俗易懂]
- Introduction to Dynamic Planning II (5.647.62)
猜你喜欢
【Experience Cloud】如何在VsCode中取得Experience Cloud的MetaData
Dimension table and fact table in data warehouse
2020.4.12 byte written test questions B DP D monotone stack
[development environment] install Visual Studio Ultimate 2013 development environment (download software | install software | run software)
Crawl the information of national colleges and universities in 1 minute and make it into a large screen for visualization!
Soul torture, what is AQS???
Thoroughly understand browser strong cache and negotiation cache
Xpt2046 four wire resistive touch screen
《大学“电路分析基础”课程实验合集.实验七》丨正弦稳态电路的研究
《大学“电路分析基础”课程实验合集.实验四》丨线性电路特性的研究
随机推荐
floyed「建议收藏」
Introduction to Dynamic Planning II (5.647.62)
[idea] recommend an idea translation plug-in: translation "suggestions collection"
数字藏品系统开发(程序开发)丨数字藏品3D建模经济模式系统开发源码
Experiment collection of University "Fundamentals of circuit analysis". Experiment 7 - Research on sinusoidal steady-state circuit
可视化技术在 Nebula Graph 中的应用
/Bin/ld: cannot find -lgssapi_ krb5
《大学“电路分析基础”课程实验合集.实验五》丨线性有源二端网络等效电路的研究
Pattern matching extraction of specific subgraphs in graphx graph Computing Practice
Lseek error
Use ffmpeg command line to push UDP and RTP streams (H264 and TS), and ffplay receives
2303. Calculate the total tax payable
SQL FOREIGN KEY
PHP static members
【idea】推荐一个idea翻译插件:Translation「建议收藏」
How to use percona tool to add fields to MySQL table after interruption
(Wanzi essence knowledge summary) basic knowledge of shell script programming
华为云服务器安装mysqlb for mysqld.service failed because the control process exited with error code.See “sys
中科大脑知识图谱平台建设及业务实践
Teach you how to build virtual machines locally and deploy microservices