QJsonDocument({"ble_sw":1,"etc_pwr_gain":0,"etc_sw":1,"lat":"456","lot":"123","ntpsvr":"798:123","wifi_sw":1}) 使用c++ 在qt中解析,给出代码
时间: 2023-10-14 22:05:59 浏览: 122
可以使用QJsonDocument和QJsonObject来解析该JSON字符串。以下是示例代码:
```c++
#include <QJsonDocument>
#include <QJsonObject>
#include <QDebug>
QString jsonString = "{\"ble_sw\":1,\"etc_pwr_gain\":0,\"etc_sw\":1,\"lat\":\"456\",\"lot\":\"123\",\"ntpsvr\":\"798:123\",\"wifi_sw\":1}";
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8());
QJsonObject jsonObj = jsonDoc.object();
int bleSw = jsonObj.value("ble_sw").toInt();
int etcPwrGain = jsonObj.value("etc_pwr_gain").toInt();
int etcSw = jsonObj.value("etc_sw").toInt();
QString lat = jsonObj.value("lat").toString();
QString lot = jsonObj.value("lot").toString();
QString ntpsvr = jsonObj.value("ntpsvr").toString();
int wifiSw = jsonObj.value("wifi_sw").toInt();
qDebug() << "ble_sw:" << bleSw;
qDebug() << "etc_pwr_gain:" << etcPwrGain;
qDebug() << "etc_sw:" << etcSw;
qDebug() << "lat:" << lat;
qDebug() << "lot:" << lot;
qDebug() << "ntpsvr:" << ntpsvr;
qDebug() << "wifi_sw:" << wifiSw;
```
输出结果为:
```
ble_sw: 1
etc_pwr_gain: 0
etc_sw: 1
lat: "456"
lot: "123"
ntpsvr: "798:123"
wifi_sw: 1
```
阅读全文