QJsonObject返回null

时间:2019-04-12 22:33:43

标签: c++ json qt

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变量存储空值。

1 个答案:

答案 0 :(得分:1)

要理解,最好使用以下格式查看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。 在这种情况下,您必须访问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}])