Qt工作笔记-使用toVariant().toMap()分割Json文件(666解析法)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Qt工作笔记-使用toVariant().toMap()分割Json文件(666解析法)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                目錄
?
?
概念
演示過程及源碼
?
概念
今天看到大佬去解析Json文件的代碼,簡直666,特意記錄此筆記,方便以后用得到,肯定會用得到。
?
演示過程及源碼
如下面的這個Json代碼,這個是調用face++的http接口,回的數據:
{"faces1": [{"face_rectangle": {"width": 173, "top": 56, "left": 221, "height": 173}, "face_token": "74d5ab683973850bd8ae8171bd5e2dc0"}], "faces2": [{"face_rectangle": {"width": 97, "top": 82, "left": 58, "height": 97}, "face_token": "05a3a9d4356840a05aa067759bf36a65"}], "time_used": 706, "thresholds": {"1e-3": 62.327, "1e-5": 73.975, "1e-4": 69.101}, "confidence": 88.364, "image_id2": "cwCS2E2mgKybwHt76JDnlA==", "image_id1": "CTnCybS3mzNADgybps4lwQ==", "request_id": "1550108144,bad5f86c-8b34-4a29-8994-bf68cbef6bce"}給出一個截圖:
Qt里面有個QVariant神器,利用QVariant這個神器去存儲數據!!!
?
源碼如下:
#include <QFile> #include <QDebug> #include <QTextStream> #include <QJsonDocument> #include <QMap> #include <QByteArray> #include <QVariant>int main(int argc, char *argv[]) {QFile file("E:\\Qt2019\\JsonToMapDemo\\test.txt");if(!file.open(QIODevice::ReadOnly | QIODevice::Text))return -1;QTextStream in(&file);QString allData = in.readAll();QMap<QString, QVariant> map;QJsonDocument document;qDebug() << allData.toLatin1();map = document.fromJson(allData.toLatin1()).toVariant().toMap();qDebug() << map.value("confidence").toFloat();file.close();return 0; }這里來看看allData.toLatin1()轉換成map后,存儲的東西:
?
?
?
總結
以上是生活随笔為你收集整理的Qt工作笔记-使用toVariant().toMap()分割Json文件(666解析法)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Leaflet笔记-把leaflet-t
- 下一篇: Python笔记-解决开启Fiddler
