0

I have this following JSON request(only a snippet of it) and I am trying to parse the temperatureHigh in data[{}]. I can't figure out how to parse an array of objects thats inside an object. Im using 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,

So far this is what I have

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

QVariantMap qVar1 = jObj.value("daily").toVariant().toMap();
5
  • The full JSON request can be seen at api.darksky.net/forecast/8fd6289f02cec9a891081d5e0ac5675c/… Commented Mar 16, 2019 at 22:28
  • Do you just need to use qVar1["temperatureHigh"].toDouble()? Commented Mar 16, 2019 at 22:31
  • @Ian4264 No that doesn't work. qVar1 only sees summary, icon, and data Commented Mar 16, 2019 at 22:34
  • Ah yes, didn't spot the brackets. I think you need something like qVar1["data"].toArray().front() ["temperatureHigh"] Commented Mar 16, 2019 at 22:49
  • 1
    Be careful with your quotation marks: is different from ". Commented Mar 17, 2019 at 1:02

1 Answer 1

1

This is the minimal JSON sample I worked with:

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

Assuming that you've already read or stored your JSON into a QByteArray instance called 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;

Output:

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

In one line:

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

Some variants you might like to opt for (by taking advantage of QJsonValue::operator[] overloads):

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"];

Really, the only classes and types you should be conscious of while traversing your JSON are QJsonObject, QJsonArray, and QJsonValue. (There's not really any need to use to QVariant or QVariantMap.) Read the docs, write more code, and you'll get used to it. :-)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.