QT解析json

QT解析json

QJsonDocument结合QVariant进行解析

 1 {
 2     "bsType": 0,
 3     "setId": 1000,
 4     "mainInfo": [
 5         {
 6             "cpuType": "i7-8550",
 7             "coreNum": 8,
 8             "mainFrequency": "2.4GHz",
 9             "diskType": "Cme-China",
10             "diskTotolSize": 1024,
11             "disks":[   
12                 {"diskName": "C:", "diskSize":256, "usedSize":120, "usedRatio":0.6, "uflag":0},
13                 {"diskName": "D:", "diskSize":256, "usedSize":100, "usedRatio":0.2, "uflag":1},
14                 {"diskName": "E:", "diskSize":512, "usedSize":300, "usedRatio":0.2, "uflag":0}
15             ]                
16         }
17     ]
18 }
View Code

解析json实例

 1 #include <QCoreApplication>
 2 #include <QFile>
 3 #include <QJsonDocument>
 4 #include <QVariantMap>
 5 #include <QDateTime>
 6 #include <QJsonObject>
 7 #include <QDebug>
 8 
 9 QByteArray readJson(const QString &jsonFile)
10 {
11     QFile file(jsonFile);
12     if(file.open(QIODevice::ReadOnly) == false)
13     {
14         return QByteArray("");
15     }
16 
17     QByteArray jsonStr = file.readAll();
18     file.close();
19     return jsonStr;
20 }
21 
22 int main(int argc, char *argv[])
23 {
24     QCoreApplication a(argc, argv);
25     QByteArray bytearray = readJson("./Untitled.json");
26     QJsonParseError jsonErr;
27     QJsonDocument jsonDoc = QJsonDocument::fromJson(bytearray, &jsonErr);
28     if(jsonErr.error != QJsonParseError::NoError)
29     {
30         qDebug()<<QString::fromLocal8Bit("Json文件解析失败:");
31         return a.exec();
32     }
33 // qDebug() << jsonDoc.toVariant().toMap()["mainInfo"];
34 // QVariantMap jsonMap = jsonDoc.toVariant().toMap();//当前那一层是{}使用
35 // QList<QVariant> MonitorList = jsonDoc.toVariant().toList();//当前那一层是数组[]的时候使用
36     QList<QVariant> MonitorList = jsonDoc.toVariant().toMap()["mainInfo"].toList();
37     for(auto &MonitorItem : MonitorList)
38     {
39         QVariantMap MonitorMap = MonitorItem.toMap();
40         qDebug() << MonitorMap["cpuType"];
41         qDebug() << MonitorMap["coreNum"];
42         qDebug() << MonitorMap["mainFrequency"];
43         qDebug() << MonitorMap["diskType"];
44         qDebug() << MonitorMap["diskTotolSize"];
45 // qDebug() << MonitorMap["disks"];
46         QList<QVariant> slist = MonitorMap["disks"].toList();
47         for (auto &item: slist)
48         {
49             QVariantMap sMap = item.toMap();
50             qDebug() << sMap["diskName"];
51             qDebug() << sMap["diskSize"];
52             qDebug() << sMap["usedSize"];
53             qDebug() << sMap["usedRatio"];
54             qDebug() << sMap["uflag"];
55         }
56     }
57     return a.exec();
58 }
View Code

json的写入

1 QVariantMap map;  
2 map["address"] = QString("192.168.0.1");  
3 map["name"] = QString("xie");  
4 
5 QString data = QJsonDocument::fromVariant(map).toJson(QJsonDocument::Indented);//带有格式。  
6                                             QJsonDocument::Compact(没有格式)
View Code

 

posted @ 2019-07-24 14:31  不知云深处  阅读(1488)  评论(0编辑  收藏  举报