Qt解析对象数组JSON

时间:2019-03-16 22:08:36

标签: c++ json qt parsing

我有以下JSON请求(仅是其中的摘要),我正在尝试解析data [{}]中的temperatureHigh。我无法弄清楚如何解析对象内部的对象数组。我正在使用Qt。

{    
    "latitude":xxx,
    “longitude":xxx,
    “timezone":"America/New_York",
    “currently":{
    "time":1552765335,
    “summary":"Clear",
    “icon":"clear-day",
    “nearestStormDistance":4,
    “nearestStormBearing":268,
    “precipIntensity":0,
    “precipProbability":0,
    “temperature":48.32,
    “apparentTemperature":43.25,
    “dewPoint":20.91,
    “humidity":0.33,
    “pressure":1014.29,
    “windSpeed":12.22,
    “windGust":20.11,
    “windBearing":310,
    “cloudCover":0.04,
    “uvIndex":3,
    “visibility":9.64,
    “ozone":317.85
    },
    “daily":{
        "summary":"No precipitation throughout the week, with high temperatures falling to 43°F on Tuesday.",
        “icon":"clear-day",
        “data":[{
            "time":1552708800,
            “summary":"Partly cloudy until afternoon.",
            “icon":"partly-cloudy-day",
            “sunriseTime":1552734266,
            “sunsetTime":1552777293,
            “moonPhase":0.34,
            “precipIntensity":0.0007,
            “precipIntensityMax":0.0101,
            “precipIntensityMaxTime":1552708800,
            “precipProbability":0.35,
            “precipType":"rain",
            “temperatureHigh":48.89,
            “temperatureHighTime":1552759200,
            “temperatureLow":31.84,
            “temperatureLowTime":1552820400,
            “apparentTemperatureHigh":43.85,
            “apparentTemperatureHighTime":1552762800,
            “apparentTemperatureLow":22.64,
            “apparentTemperatureLowTime":1552820400,
            “dewPoint":29.06,
            “humidity":0.52,

到目前为止,这就是我拥有的

QJsonParseError jError;
QJsonDocument test = QJsonDocument::fromJson(data, &jError);

QVariantMap qVar1 = jObj.value("daily").toVariant().toMap();

1 个答案:

答案 0 :(得分:1)

这是我使用过的最小的JSON示例:

{
    "humidity": 0.33,
    "pressure": 1014.29,
    "daily": {
        "data": [
            {
                "temperatureHigh": 48.89,
                "temperatureLow": 31.84
            }
        ]
    }
}

假设您已经将JSON读取或存储到名为QByteArray的{​​{1}}实例中:

json

输出:

QJsonParseError err;
auto doc = QJsonDocument::fromJson(json, &err);           // parse the json
if (err.error != QJsonParseError::NoError)
    qDebug() << err.errorString();

QJsonObject obj = doc.object();                                  // get the object
qDebug() << "obj:" << obj;

QJsonObject daily = obj.value("daily").toObject();               // get the daily value as an object     
qDebug() << "daily:" << daily;

QJsonArray data = daily.value("data").toArray();                // get the data value as an array    
qDebug() << "data:" << data;

QJsonObject first = data[0].toObject();                          // get the first value as an object    
qDebug() << "first:" << first;

double temperatureHigh = first.value("temperatureHigh").toDouble(); // get the temperatureHigh value    
qDebug() << "temperatureHigh:" << temperatureHigh;

一行:

obj: QJsonObject({"daily":{"data":[{"temperatureHigh":48.89,"temperatureLow":31.84}]},"humidity":0.33,"pressure":1014.29})
daily: QJsonObject({"data":[{"temperatureHigh":48.89,"temperatureLow":31.84}]})
data: QJsonArray([{"temperatureHigh":48.89,"temperatureLow":31.84}])
first: QJsonObject({"temperatureHigh":48.89,"temperatureLow":31.84})
temperatureHigh: 48.89

您可能希望选择的某些变体(通过利用QJsonValue::operator[] overloads):

auto temperatureHigh = doc.object().value("daily").toObject().value("data").toArray()[0].toObject().value("temperatureHigh");

确实,遍历JSON时,您应注意的唯一类和类型是QJsonObjectQJsonArrayQJsonValue。 (实际上并不需要使用auto temperatureHigh = doc.object().value("daily")["data"][0].toObject()["temperatureHigh"]; ---- const auto obj = doc.object(); // obj must be const to call the appropriate overload that returns QJsonValue auto temperatureHigh = obj["daily"]["data"][0]["temperatureHigh"]; QVariant。)阅读文档,编写更多代码,您会习惯的。 :-)