当前位置:网站首页>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 !!!!
边栏推荐
- 《大学“电路分析基础”课程实验合集.实验六》丨典型信号的观察与测量
- /Bin/ld: cannot find -lxml2
- The task cannot be submitted after the installation of flick is completed
- floyed「建议收藏」
- /bin/ld: 找不到 -lpam
- Aike AI frontier promotion (7.2)
- (5) Flink's table API and SQL update mode and Kafka connector case
- (万字精华知识总结)Shell脚本编程基础知识
- 爱可可AI前沿推介(7.2)
- Armv8-a programming guide MMU (4)
猜你喜欢
idea jar包冲突排查
全是精华的模电专题复习资料:基本放大电路知识点
Introduction to Dynamic Planning II (5.647.62)
HMS core machine learning service helps zaful users to shop conveniently
《大学“电路分析基础”课程实验合集.实验五》丨线性有源二端网络等效电路的研究
The outline dimension function application of small motherboard
动态规划入门二(5.647.62)
树-二叉搜索树
How to use percona tool to add fields to MySQL table after interruption
Processing gzip: stdin: not in gzip format: child returned status 1tar: error is not recoverable: exitin
随机推荐
《大学“电路分析基础”课程实验合集.实验七》丨正弦稳态电路的研究
数组和链表的区别浅析
[idea] recommend an idea translation plug-in: translation "suggestions collection"
Why does the system convert the temp environment variable to a short file name?
2020.4.12 byte written test questions B DP D monotone stack
(5) Flink's table API and SQL update mode and Kafka connector case
QVariant与Json的各种纠葛——Qt
《大学“电路分析基础”课程实验合集.实验四》丨线性电路特性的研究
目标检测—利用labelimg制作自己的深度学习目标检测数据集
如何實現十億級離線 CSV 導入 Nebula Graph
【小白聊云】中小企业容器化改造建议
2278. Percentage of letters in string
将点云坐标转换成世界坐标的demo
全方位解读服务网格(Service Mesh)的背景和概念
Add an empty column to spark dataframe - add an empty column to spark dataframe
/Bin/ld: cannot find -lxml2
fastjson List转JSONArray以及JSONArray转List「建议收藏」
How to import a billion level offline CSV into Nepal graph
Application of visualization technology in Nebula graph
【5G NR】RRC连接释放