简体   繁体   中英

QJsonObject returns null

QJsonObject does not return values normally.

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}}}

Code

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

I expected OK to be stored in the status variable, but it did not contain anything.

The items variable stores null.

To understand is better to see the json using the following format:

{
    "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 is a format where each element has a hierarchy, that is to say to access an element you have to do it through the parents.

In the case resultMsg you have to access response -> header-> resultMsg . In the case item you have to access response -> body-> items-> item .

Considering the above, the solution is:

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;

Output:

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}])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM