gpt4 book ai didi

c++ - QJsonObject 返回 null

转载 作者:行者123 更新时间:2023-11-30 04:48:54 32 4
gpt4 key购买 nike

QJsonObject 不正常返回值。

json

{"response":{"header":{"resultCode":"0000","resultMsg":"OK"},"body":{"items":{"item":[{"baseDate":20190413,"baseTime":"0600","category":"PTY","nx":55,"ny":127,"obsrValue":0},{"baseDate":20190413,"baseTime":"0600","category":"REH","nx":55,"ny":127,"obsrValue":-998},{"baseDate":20190413,"baseTime":"0600","category":"RN1","nx":55,"ny":127,"obsrValue":0},{"baseDate":20190413,"baseTime":"0600","category":"T1H","nx":55,"ny":127,"obsrValue":6.3},{"baseDate":20190413,"baseTime":"0600","category":"UUU","nx":55,"ny":127,"obsrValue":0},{"baseDate":20190413,"baseTime":"0600","category":"VEC","nx":55,"ny":127,"obsrValue":0},{"baseDate":20190413,"baseTime":"0600","category":"VVV","nx":55,"ny":127,"obsrValue":0},{"baseDate":20190413,"baseTime":"0600","category":"WSD","nx":55,"ny":127,"obsrValue":0}]},"numOfRows":10,"pageNo":1,"totalCount":8}}}

代码

QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll());
QJsonObject jsonObj = jsonDoc.object();
QString status = jsonObj["resultMsg"].toString(); // empty
QJsonArray items = jsonObj["item"].toArray(); // null

我希望 OK 存储在状态变量中,但它没有包含任何内容。

items 变量存储 null。

最佳答案

为了更好地理解,请使用以下格式查看 json:

{
"response": {
"header": {
"resultCode": "0000",
"resultMsg": "OK"
},
"body": {
"items": {
"item": [{
"baseDate": 20190413,
"baseTime": "0600",
"category": "PTY",
"nx": 55,
"ny": 127,
"obsrValue": 0
}, {
"baseDate": 20190413,
"baseTime": "0600",
"category": "REH",
"nx": 55,
"ny": 127,
"obsrValue": -998
}, {
"baseDate": 20190413,
"baseTime": "0600",
"category": "RN1",
"nx": 55,
"ny": 127,
"obsrValue": 0
}, {
"baseDate": 20190413,
"baseTime": "0600",
"category": "T1H",
"nx": 55,
"ny": 127,
"obsrValue": 6.3
}, {
"baseDate": 20190413,
"baseTime": "0600",
"category": "UUU",
"nx": 55,
"ny": 127,
"obsrValue": 0
}, {
"baseDate": 20190413,
"baseTime": "0600",
"category": "VEC",
"nx": 55,
"ny": 127,
"obsrValue": 0
}, {
"baseDate": 20190413,
"baseTime": "0600",
"category": "VVV",
"nx": 55,
"ny": 127,
"obsrValue": 0
}, {
"baseDate": 20190413,
"baseTime": "0600",
"category": "WSD",
"nx": 55,
"ny": 127,
"obsrValue": 0
}]
},
"numOfRows": 10,
"pageNo": 1,
"totalCount": 8
}
}
}

Json 是一种格式,其中每个元素都有一个层次结构,也就是说要访问一个元素,您必须通过父元素来完成。

在 resultMsg 的情况下,您必须访问 response -> header-> resultMsg。在 case item 中,您必须访问 response -> body-> items-> item

综合以上,解决方案是:

QJsonArray item_array;
QString status;

QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll());

QJsonObject jsonObj = jsonDoc.object();
if(jsonObj.contains("response")){
QJsonObject response_obj = jsonObj["response"].toObject();
if(response_obj.contains("header")){
QJsonObject header_obj = response_obj["header"].toObject();
if(header_obj.contains("resultMsg")){
status = header_obj["resultMsg"].toString();
}
}
if(response_obj.contains("body")){
QJsonObject body_obj = response_obj["body"].toObject();
if(body_obj.contains("items")){
QJsonObject items_obj = body_obj["items"].toObject();
if(items_obj.contains("item")){
item_array = items_obj["item"].toArray();
}
}
}
}
qDebug()<< "status:" << status;
qDebug()<< "item:" << item_array;

输出:

status: "OK"
item: QJsonArray([{"baseDate":20190413,"baseTime":"0600","category":"PTY","nx":55,"ny":127,"obsrValue":0},{"baseDate":20190413,"baseTime":"0600","category":"REH","nx":55,"ny":127,"obsrValue":-998},{"baseDate":20190413,"baseTime":"0600","category":"RN1","nx":55,"ny":127,"obsrValue":0},{"baseDate":20190413,"baseTime":"0600","category":"T1H","nx":55,"ny":127,"obsrValue":6.3},{"baseDate":20190413,"baseTime":"0600","category":"UUU","nx":55,"ny":127,"obsrValue":0},{"baseDate":20190413,"baseTime":"0600","category":"VEC","nx":55,"ny":127,"obsrValue":0},{"baseDate":20190413,"baseTime":"0600","category":"VVV","nx":55,"ny":127,"obsrValue":0},{"baseDate":20190413,"baseTime":"0600","category":"WSD","nx":55,"ny":127,"obsrValue":0}])

关于c++ - QJsonObject 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55660212/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com