当前位置:网站首页>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 !!!!
边栏推荐
猜你喜欢
XPT2046 四线电阻式触摸屏
Experiment collection of University "Fundamentals of circuit analysis". Experiment 4 - Research on linear circuit characteristics
Introduction to dynamic planning I, BFS of queue (70.121.279.200)
【Experience Cloud】如何在VsCode中取得Experience Cloud的MetaData
Nebula Graph & 数仓血缘关系数据的存储与读写
Pattern matching extraction of specific subgraphs in graphx graph Computing Practice
Experiment collection of University "Fundamentals of circuit analysis". Experiment 7 - Research on sinusoidal steady-state circuit
GraphX 图计算实践之模式匹配抽取特定子图
[experience cloud] how to get the metadata of experience cloud in vscode
如何实现十亿级离线 CSV 导入 Nebula Graph
随机推荐
数据湖(十一):Iceberg表数据组织与查询
XPT2046 四线电阻式触摸屏
使用FFmpeg命令行进行UDP、RTP推流(H264、TS),ffplay接收
Ant group's large-scale map computing system tugraph passed the national evaluation
处理gzip: stdin: not in gzip formattar: Child returned status 1tar: Error is not recoverable: exitin
(5) Flink's table API and SQL update mode and Kafka connector case
PostgresSQL 流复制 主备切换 主库无读写宕机场景
基于 Nebula Graph 构建百亿关系知识图谱实践
【Salesforce】如何确认你的Salesforce版本?
6096. 咒语和药水的成功对数
PyObject 转 char* (string)
win10系统升级一段时间后,内存占用过高
6096. Success logarithm of spells and potions
Flink real-time data warehouse (7): Flink realizes the full pull module to extract data in MySQL
beforeEach
Experiment collection of University Course "Fundamentals of circuit analysis". Experiment 5 - Research on equivalent circuit of linear active two terminal network
Ssh/scp does not prompt all activities are monitored and reported
【Experience Cloud】如何在VsCode中取得Experience Cloud的MetaData
6090. Minimax games
二叉树前,中,后序遍历