01-02-2013 01:01 AM
I have the following data:
QVariant(QVariantMap, QMap(("data", QVariant(QVariantMap, QMap(("current_condition", QVariant(QVariantList, (QVariant(QVariantMap, QMap............QVariant(QVariantMap, QMap(("weather", QVariant(QVariantList, (QVariant(QVariantMap, QMap...........QVariant(QVariantMap, QMap(("weather_desc", QVariant(QVariantList, (QVariant(QVariantMap, QMap...........
m_model->insert(data.value<QVariantMap>().value("data").toMap().value("weather").toList().at(0).toMap());
By using the above line, I am able to extract relevant data from "current_condition and "weather".
What would I need to add to the above line to further extract data from "weather_desc"?
Any help would be appreciated. Thank you.
Solved! Go to Solution.
01-02-2013 06:56 AM
Could you clarify what is this? I cannot understand what is your data.
01-03-2013 04:17 AM
Could you provide whole data without dots please?
01-03-2013 09:39 PM
{ "data": { "current_condition": [ {"cloudcover": "0", "humidity": "18", "observation_time": "02:36 AM", "precipMM": "0.0", "pressure": "1007", "temp_C": "39", "temp_F": "102", "visibility": "10", "weatherCode": "113", "weatherDesc": [ {"value": "Sunny" } ], "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.pn g" } ], "winddir16Point": "N", "winddirDegree": "10", "windspeedKmph": "46", "windspeedMiles": "29" } ], "request": [ {"query": "Melbourne, Australia", "type": "City" } ], "weather": [ {"date": "2013-01-04", "precipMM": "0.0", "tempMaxC": "37", "tempMaxF": "98", "tempMinC": "19", "tempMinF": "66", "weatherCode": "113", "weatherDesc": [ {"value": "Sunny" } ], "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.pn g" } ], "winddir16Point": "N", "winddirDegree": "3", "winddirection": "N", "windspeedKmph": "21", "windspeedMiles": "13" } ] }}
I just needed to extract data from "current_condition" , "weather" and "weatherDesc".
I was able to extract from one at a time but not all three at once. just require the relevant code. Thank you.
01-03-2013 09:52 PM - edited 01-03-2013 10:03 PM
Thanks! It's much easier to read when formatted, so that's the first thing I did. ![]()
{ "data":
{
"current_condition":
[
{
"cloudcover": "0",
"humidity": "18",
"observation_time": "02:36 AM",
"precipMM": "0.0",
"pressure": "1007",
"temp_C": "39",
"temp_F": "102",
"visibility": "10",
"weatherCode": "113",
"weatherDesc": [ {"value": "Sunny" } ],
"weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.pn g" } ],
"winddir16Point": "N",
"winddirDegree": "10",
"windspeedKmph": "46",
"windspeedMiles": "29"
}
],
"request":
[
{
"query": "Melbourne, Australia",
"type": "City"
}
],
"weather":
[
{
"date": "2013-01-04",
"precipMM": "0.0",
"tempMaxC": "37",
"tempMaxF": "98",
"tempMinC": "19",
"tempMinF": "66",
"weatherCode": "113",
"weatherDesc": [ {"value": "Sunny" } ],
"weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.pn g" } ],
"winddir16Point": "N",
"winddirDegree": "3",
"winddirection": "N",
"windspeedKmph": "21",
"windspeedMiles": "13"
}
]
}
}
To access weatherDesc in weather use:
data.value<QVariantMap>().value("data").toMap().va
This will return "Sunny".
For better readability I suggest splitting this string into:
QMap<QString, QVariant> dataMap = data.value<QVariantMap>().value("data").toMap();
QList<QVariant> weatherList = dataMap..value("weather").toList();
QVariant firstWeatherEntry = weatherList.at(0);
QMap<QString, QVariant> weatherEntryMap = firstWeatherEntry.toMap();
QList<QVariant> weatherDescList =
weatherEntryMap.value("weatherDesc").toList();
QVariant firstWeatherDescEntry = weatherDescList.at(0);
QMap<QString, QVariant> weatherDescEntryMap = firstWeatherDescEntry.toMap();
QString value = weatherDescEntryMap.value("value").toString(); // "Sunny"
Also you can check intermediate results at every step by calling
qDebug() << weatherList << "\n"; // and so on...
01-03-2013 11:32 PM
Thank you for some great help!
if I use the following line:
m_model->insert(data.value<QVariantMap().value("data").toMap().value("weather").toList().at(0).toMap().value("weatherDesc")
.toList().at(0).toMap());
The only data returned is "Sunny"
Any data from "weather" is undefined.
I also tried splitting the code up:
QMap<QString, QVariant> dataMap = data.value<QVariantMap>().value("data").toMap();
QList<QVariant> weatherList = dataMap..value("weather").toList();
QVariant firstWeatherEntry = weatherList.at(0);
QMap<QString, QVariant> weatherEntryMap = firstWeatherEntry.toMap();
QList<QVariant> weatherDescList =
weatherEntryMap.value("weatherDesc").toList();
QVariant firstWeatherDescEntry = weatherDescList.at(0);
QMap<QString, QVariant> weatherDescEntryMap = firstWeatherDescEntry.toMap();
QString value = weatherDescEntryMap.value("value").toString(); // "Sunny"
Returns the same value "Sunny" and all data from "weather" is undefined. I have this problem when trying to extract data from more than one array? If I just use "weather" then I am able to extract the data pertaining to it and also if I extract from "current_condition. I need to extract data from "weather" and "weatherDesc". I suppose later on "current_condition" should be retrievable?
Any suggestions?
01-04-2013 12:50 AM
I was able to successfully return value "Sunny", thank you.
If I wanted to obtain the value "37" from "tempMaxC" in "weather" and "Sunny" from "weatherDesc"
what would be required to insert these values into my GroupDataModel?
If I use m_model->insert(dataMap) then only "weather" values are extracted.
if I use m_model->insert(weatherDescEntryMap) then only "weatherDesc" value is extracted.
I need to somehow combine the two statemsnts?
QMap<QString, QVariant> dataMap = data.value<QVariantMap>().value("data").toMap(); QList<QVariant> weatherList = dataMap.value("weather").toList(); QVariant firstWeatherEntry = weatherList.at(0); QMap<QString, QVariant> weatherEntryMap = firstWeatherEntry.toMap(); QList<QVariant> weatherDescList = weatherEntryMap.value("weatherDesc").toList(); QVariant firstWeatherDescEntry = weatherDescList.at(0); QMap<QString, QVariant> weatherDescEntryMap = firstWeatherDescEntry.toMap(); QString value = weatherDescEntryMap.value("value").toString(); // "Sunny"
01-04-2013 03:19 AM
You can create combined map:
QMap<QString, QVariant> combinedMap;
combinedMap["tempMaxC"] = weatherEntryMap.value("tempMaxC");
combinedMap["weatherDesc"] = weatherDescEntryMap.value("value");
m_model->insert(combinedMap);
Then you can access tempMaxC and weatherDesc.
I hope this helps. ![]()
01-04-2013 04:58 AM
Thank you for everything! You have saved me months of heart ache!