0
使用QT5并试图分析JSON为什么不能通过qt解析这个json?
这里的功能:
void MainWindow::parse(QString &json){
QJsonDocument doc(QJsonDocument::fromJson(json.toUtf8()));
QJsonObject obj = doc.object();
QJsonArray result = obj["results"].toArray();
QJsonValue location =result.at(0);
QJsonValue now = result.at(1);
QJsonValue time = result.at(2);
cityName = location.toObject().take("name").toString();
status = now.toObject().take("text").toString();
qDebug()<<time.toString(); // this qdebug is for testing
}
的JSON的QString看起来是这样的:
{
"results": [
{
"location": {
"id": "WX4FBXXFKE4F",
"name": "北京",
"country": "CN",
"path": "北京,北京,中国",
"timezone": "Asia/Shanghai",
"timezone_offset": "+08:00"
},
"now": {
"text": "晴",
"code": "0",
"temperature": "-4"
},
"last_update": "2016-12-09T23:25:00+08:00"
}
]
}
我期待qDebug
输出为"2016-12-09T23:25:00+08:00"
,但它只是""
而且cityname
和status
竟然设置为""
。
这里有什么问题?谢谢!
你有没有检查'result.size()' ?你是否尝试通过并检查'QJsonDocument :: fromJson'中的'QJsonParseError * error'? – Jarod42
用调试器遍历代码并检查变量值。如果你不能通过它来弄明白,那么在每个语句之间添加调试打印,并用该代码编辑问题,并且它是完整的输出。 – hyde