Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
robtraf
Posts: 48
Registered: ‎03-27-2011
My Device: BlackBerry Z10
Accepted Solution

Extracting data from QVariantMap

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.

 

 

Please use plain text.
Developer
tonygluk
Posts: 30
Registered: ‎11-06-2012
My Device: BB 10 Dev Alpha

Re: Extracting data from QVariantMap

Could you clarify what is this? I cannot understand what is your data.

Please use plain text.
Developer
Developer
klz
Posts: 132
Registered: ‎08-24-2011
My Device: Z10 LE + Playbook

Re: Extracting data from QVariantMap

Could you provide whole data without dots please? 

Thank you!
----------------------------
My Apps: Alchemy Game | Image Resizer | Jam! | Traffic Signs: USA | Radio Record
My Blog: http://bb10pro.com
Please use plain text.
Developer
robtraf
Posts: 48
Registered: ‎03-27-2011
My Device: BlackBerry Z10

Re: Extracting data from QVariantMap

{ "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.png" } ], "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.png" } ], "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.
Please use plain text.
Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: Extracting data from QVariantMap

[ Edited ]

Thanks! It's much easier to read when formatted, so that's the first thing I did. :smileyhappy:

 

{ "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.png" } ],
        "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.png" } ],
        "winddir16Point": "N", 
        "winddirDegree": "3",
        "winddirection": "N",
        "windspeedKmph": "21",
        "windspeedMiles": "13" 
      }
    ]
  }
}

To access weatherDesc in weather use:

 

data.value<QVariantMap>().value("data").toMap().value("weather").toList().at(0).toMap().value("weatherDesc").toList().at(0).toMap().value("value") 

 

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

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Developer
robtraf
Posts: 48
Registered: ‎03-27-2011
My Device: BlackBerry Z10

Re: Extracting data from QVariantMap

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?

Please use plain text.
Developer
robtraf
Posts: 48
Registered: ‎03-27-2011
My Device: BlackBerry Z10

Re: Extracting data from QVariantMap

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"

 

Please use plain text.
Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: Extracting data from QVariantMap

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. :smileyhappy:


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Developer
robtraf
Posts: 48
Registered: ‎03-27-2011
My Device: BlackBerry Z10

Re: Extracting data from QVariantMap

Thank you for everything! You have saved me months of heart ache!

Please use plain text.