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
My Carrier: Telstra

How to iterate thru Json parsed file and retrieve key:value pairs.

I have the following Json file output. I have tried all examples in documentation on how to find/extract key::value from within the data.

I need guidance (code) as to how I can populate a GroupDataModule with the key:value properties. I can see that its a QMap with array (list), just struggling to incorporate code to iterate thru the "data" and obtain key:value pairs. Any help would be greatly appreciated.

 

  jsonMap = jsonData.toMap().find("data")->toMap();  // jsonMap contains the following:

 

{

     "data":{

          "condition":[

.                   {

                     "speedr":"10",

                     "temp":"23",

                     "time":"12:29 PM",

                     "tyre_pressure":"33.6",

                     "visibility":"10"

                     "trackDesc":[

.                                     {

                               "value":"Clear"

.                                     }

                     ],

                     "trackPicture":[

.                                     {

                           "value":"track.png"

.                                     }

                     ],

                     "trackPos":"N",

                     "trackDegree":"360",

                     "trackAngle":"11"

.                   }

          ],

          "people":[

.                   {

                     "date":"2012-10-03",

                     "numbPeop":"1000",

                     "personCode":"M",

                     "personDesc":"Slight"

.                   }

          ]

     }

}

 

I have come close with the followin code:

      for (QMap<QString, QVariant>::iterator it = jsonMap.begin(); it != jsonMap.end(); it++) {......

Just require some help!

Please use plain text.
Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: How to iterate thru Json parsed file and retrieve key:value pairs.

You could always just insert the qvariantmap into the datamodel, is there a reason you wouldnt want to do that?

 

Also, I would stay away from find()->toMap because find has the possibility of returning a QList which would have problems.

Like all of my posts
Please use plain text.
Developer
robtraf
Posts: 48
Registered: ‎03-27-2011
My Device: BlackBerry Z10
My Carrier: Telstra

Re: How to iterate thru Json parsed file and retrieve key:value pairs.

i will give it a try and see what happens. Thanks for your quick reply.
Please use plain text.
Developer
robtraf
Posts: 48
Registered: ‎03-27-2011
My Device: BlackBerry Z10
My Carrier: Telstra

Re: How to iterate thru Json parsed file and retrieve key:value pairs.

From the following Json:

 

{

     "data":{

          "condition":[

.                   {

                     "speedr":"10",

                     "temp":"23",

                     "time":"12:29 PM",

                     "tyre_pressure":"33.6",

                     "visibility":"10"

                     "trackDesc":[

.                                     {

                               "value":"Clear"

.                                     }

                     ],

                     "trackPicture":[

.                                     {

                           "value":"track.png"

.                                     }

                     ],

                     "trackPos":"N",

                     "trackDegree":"360",

                     "trackAngle":"11"

.                   }

          ],

          "people":[

.                   {

                     "date":"2012-10-03",

                     "numbPeop":"1000",

                     "personCode":"M",

                     "personDesc":"Slight"

.                   }

          ]

     }

}

 

When I display the above in qDebug via the console, the Json looks like this:

 

QVariant(QVariantMap, QMap(("data", QVariant(QVariantMap, QMap(("condition", QVariant(QVarliantList, (QVariant(QVariantMap, QMap(("speed", QVariant(QString, "10") ) ( "temp" , QVariant(QString, "23") ) ( "time" , QVariant(QString, "12:29 PM") ) (..........

 

Do I just insert this into the GroupDataModel? Or do I need to iterate thru the Json and then insert values?

If I just wanted to use speed = 10, temp = 23, time = 12:29 PM and any other values, what code should I use? Thank you.

Please use plain text.
Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: How to iterate thru Json parsed file and retrieve key:value pairs.

You would just insert the map. If you did model->insert(json.value("data").toMap()).

 

Then from qml, you would access it like ListItemData.condition.speed or ListItemData.condition.temp assuming that you were using your own custom ListItemComponent

Like all of my posts
Please use plain text.
Developer
robtraf
Posts: 48
Registered: ‎03-27-2011
My Device: BlackBerry Z10
My Carrier: Telstra

Re: How to iterate thru Json parsed file and retrieve key:value pairs.

Thank you for your solution. Unfortunately i still get an empty model when trying to populate the groupdatamodel.

 

Using qDebug I am presented with the following output from the Json load -

 

QVariant(QVariantMap, QMap(("data", QVariant(QVariantMap, QMap(("condition", QVariant(QVarliantList, (QVariant(QVariantMap, QMap(("speed", QVariant(QString, "10") ) ( "temp" , QVariant(QString, "23") ) ( "time" , QVariant(QString, "12:29 PM") ) (..........

 

If, for example, i was only interested in inserting the temp:23 into my model, would i need to somehow use code similar to the following -

 

QVariantMap map;

  map["temp"] = QVariant(QString());

  model->insert(map);

 

I tried this but I do not get the string value 23. Possibly not quite understanding Json files as mine is an object.

 

The root element of the source JSON data needs to be either an array or an object. The JSON data that's presented above uses an object (map) as the root element, so it's a valid source to use with JsonDataAccess.

I cannot find an example where the root element is an object (map), there are many examples of where the root element is an array (list).

 

From the documentation - There are two JSON element types that are particularly applicable when you're using JsonDataAccess. An array in JSON is mapped to a QVariantList, and an object is mapped to aQVariantMap. Because the root element of your JSON data must be either an array or an object, the corresponding Qt value type will be a QVariantList or a QVariantMap (wrapped in a QVariant). Both QVariantList and QVariantMap are supported by data models in Cascades, making it easy to add data that's retrieved using JsonDataAccess directly to a data model.

 

Do I possibly need to create my own groupdatamodel, I have based mine on the weather model from the weatherguesser app. I have the QML code ready to receive my ListItemData, but cant seem to populate the model. Any assistance would be greatly appreciated once again.

 

 

Please use plain text.
Developer
greenback
Posts: 528
Registered: ‎10-17-2010
My Device: BlackBerry Z10, DAC

Re: How to iterate thru Json parsed file and retrieve key:value pairs.

HOT THREAD.:Devil2:

 

Bump.


 

Please use plain text.
Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: How to iterate thru Json parsed file and retrieve key:value pairs.

What do you mean empty model? If you insert something into the model then it is not empty. You example code there would obviously have no value since you just inserted an empty string. I think you are making this way harder than it is.

 

Is that JSON the whole file? Is all you want to do insert temp? Why not just put the whole data object in there, or each condition? Condition is a json array so it may make sense just to add every condition. You also might want to post your qml since that could very easily be the problem.

 

GroupDataModel* gdm = new GroupDataModel()

gdm->setGrouping(GroupingNone) // orwhatever

 

JsonDataAccess jda

const QVariantMap json = jda.load().toMap();

const QVariantMap data = json.value("data").toMap();

foreach(const QVariant cond, data.value("condition").toList()) {

     gdm->insert(cond.toMap());

}

 

_listView->setDataModel(gdm);

 

Then in the qml, its just ListItemData.temp or ListItemData.time

Like all of my posts
Please use plain text.
Developer
robtraf
Posts: 48
Registered: ‎03-27-2011
My Device: BlackBerry Z10
My Carrier: Telstra

Re: How to iterate thru Json parsed file and retrieve key:value pairs.

Thanks. Its the whole JSON file. I was not sure on the code so i thought i would just try and get 'temp' as a start.

i really want the following:-

 "condition":[

.                   {

                     "speed":"10",                           ****** speed is the key and 10 is the value as i understand it.

                     "temp":"23",

                     "time":"12:29 PM",

                     "tyre_pressure":"33.6",

                     "visibility":"10"

                     "trackDesc":[

.........

         "people":[

.                   {

                     "date":"2012-10-03",

                     "numbPeop":"1000",

                     "personCode":"M",

                     "personDesc":"Slight"

 

Basically im trying to populate my model with the items highlited in red (hope they are red!).

 

GroupDataModel *gdm = newGroupDataModel();

JsonDataAccess jda;

QVariantMap jsonMap;

 

  if (mReply->error() == QNetworkReply::NoError)

  {

  // Parse the Json response with JsonDataAccess

  // Load the data using the reply QIODevice (buffer).

 

  QString strReply = QString::fromUtf8(mReply->readAll());

  QVariant jsonMap = jda.loadFromBuffer(strReply);

 

  const QVariantMap jsonMap = jda.load().toMap();          ******** error *****

  const QVariantMap data = json.value("data").toMap();

 

  foreach(QVariant cond, data.value("current_condition").toList()) {

      gdm->insert(cond.toMap());

  }

 

I return an error - 

Multiple markers at this line

- candidates are:

- no matching function for call to 

'bb::data::JsonDataAccess::load()'

 

The loadFromBuffer is loading the JSON ok. its somewhere after this im getting confused and making harder for myself!!!!

 

The QML is ok, as i dummied up data and it displayed ok. Its getting it off the internet into my model is the problem.

 

 

 

 

Please use plain text.
Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: How to iterate thru Json parsed file and retrieve key:value pairs.

merge these two lines..

QVariant jsonMap = jda.loadFromBuffer(strReply);

const QVariantMap jsonMap = jda.load().toMap();

into

const QVariantMap jsonMap = jda.loadFromBuffer(strReply).toMap();

Also, why are you doing "current_condition"? None of your json file has the key current_condition

Although the json structure in general is really bad (no offense if you wrote it :smileyhappy: ). Why is everything an array? Are there ever going to be more than 1 condition object in a data object? Why are trackDesc and trackPicture arrays? If possible I would simplify the whole json structure to not have so many arrays and then insert the whole data object into the data model
Like all of my posts
Please use plain text.