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
Regular Contributor
Posts: 83
Registered: ‎07-19-2012
My Device: Bold 9790, Dev Alpha A
My Carrier: Koodo
Accepted Solution

Getting to the data array in a JSON response

Hey all, this might be a bit of a long shot but here goes...

 

I've been fiddling with the Twitter Feed request example in this forum posted here and I think I've got pretty much all the code figured out. The app I'm trying to make doesn't use twitter but another API that sends a response a little bit differently.

 

Twitter sends the JSON response as an array however the JSON response I get from this *other* API has the array burried one level down. Let me explain a bit further...

 

Twitter response format

[

{name/value pairs},

{name/value pairs}, 

{name/value pairs}, and so forth

[

 

Other API response format

{name/value pair}

{name/value pair}

[

{name/value pairs},

{name/value pairs}, 

{name/value pairs}, and so forth

[

{name/value pair}

 

So you see how the array [] is one level into the JSON response? Now using the following code, how would I get into that array? Or how would I get rid of everything other than the array?

 

    // Create a group data model with id as the sorting key
    GroupDataModel* dm = new GroupDataModel(QStringList() << "id");
    dm->setGrouping(ItemGrouping::None);

    // parse the json response with JsonDataAccess
    bb::data::JsonDataAccess ja;
    QVariant jsonva = ja.loadFromBuffer(info);

    // the qvariant is an array of tweets which is extracted as a list
    QVariantList feed = jsonva.toList();

    // for each object in the array, push the variantmap in its raw form
    // into the ListView
    for (QList<QVariant>::iterator it = feed.begin(); it != feed.end(); it++)
    {
        QVariantMap tweet = it->toMap();
        dm->insert(tweet);
    }

    // set the data model to display
    list->setDataModel(dm);
    list->setVisible(true);

 It seems like it really should be obvious but I've been working all day and I can't seem to figure it out Smiley Sad I think I need to put a bit of code above the 3rd section...

 

Trusted Contributor
Posts: 140
Registered: ‎11-23-2010
My Device: Torch 9800
My Carrier: Rogers

Re: Getting to the data array in a JSON response

Ok let me help you out with this.

 

First of all I don't think that you have a list but a map. So what you would need to do is first you convert your json into a map. Then you need to find the the key you want to go to.

 

Here is a bit of my code to help you out.

 

bb::data::JsonDataAccess ja;
QVariant jsonva = ja.loadFromBuffer(info);

QVariantMap feed = jsonva.toMap().find("list")->toMap();

 

You see here I convert my json into a map then I find the key I'm looking for.

 

Hope it helps.

Trusted Contributor
Posts: 140
Registered: ‎11-23-2010
My Device: Torch 9800
My Carrier: Rogers

Re: Getting to the data array in a JSON response

This website helped me understand the structure of my json : http://json2csharp.com/

Regular Contributor
Posts: 83
Registered: ‎07-19-2012
My Device: Bold 9790, Dev Alpha A
My Carrier: Koodo

Re: Getting to the data array in a JSON response

[ Edited ]

Ok I understand now...I have an object as the JSON response and therefor must use QVariantMap. Here is the code I came up with that I've been testing to see if it works.

 

    GroupDataModel* dm = new GroupDataModel(QStringList() << "data");
    dm->setGrouping(ItemGrouping::ByFullValue);

    // parse the json response with JsonDataAccess
    bb::data::JsonDataAccess ja;
    QVariant jsonva = ja.loadFromBuffer(info);

    QVariantMap map;
    map = jsonva.toMap().find("data")->toMap();
dm->insert(map);

    // set the data model to display
    list->setDataModel(dm);
    list->setVisible(true);

Note: I changed "feed" to "map".

 

The JSON response should return a key/value pair in the object of "data":[ array ]. This is the array I'm after.

 

Now I should have the key/value pair mapped to the GroupDataModel called "dm" but I can't seem to get that outputting as a list. I feel like I need to convert that GroupDataModel into a list but I'm not exactly sure how.

Trusted Contributor
Posts: 140
Registered: ‎11-23-2010
My Device: Torch 9800
My Carrier: Rogers

Re: Getting to the data array in a JSON response

You are almost there. Since data is an array you need to iterate through that array and map each value inside, and that is what you are going to pass to the ListView Model.

 

Let me know if you want the answer to that I'll help you out.

 

1- Iterate through your array

2- Each iteration cast to a toMap()

3- dm->insert(iteration casted to a map)

 

Regular Contributor
Posts: 83
Registered: ‎07-19-2012
My Device: Bold 9790, Dev Alpha A
My Carrier: Koodo

Re: Getting to the data array in a JSON response

Oh ok, I get it now! i wouldn't mind the answer actually because it's the syntax i'm having the most trouble with. thanks a bunch!

Trusted Contributor
Posts: 140
Registered: ‎11-23-2010
My Device: Torch 9800
My Carrier: Rogers

Re: Getting to the data array in a JSON response

There you go

 

	for (QMap<QString, QVariant>::iterator it = feed.begin(); it != feed.end(); it++) {
		QVariantMap tweet = it->toMap();
		dm->insert(tweet);
	}

 

Trusted Contributor
Posts: 140
Registered: ‎11-23-2010
My Device: Torch 9800
My Carrier: Rogers

Re: Getting to the data array in a JSON response

Did it work? If it did can you mark as solution please.

Regular Contributor
Posts: 83
Registered: ‎07-19-2012
My Device: Bold 9790, Dev Alpha A
My Carrier: Koodo

Re: Getting to the data array in a JSON response

I still get no output in my ListView. Here is my code:

    // Create a group data model with id as the sorting key
    GroupDataModel* dm = new GroupDataModel(QStringList() << "data");
    dm->setGrouping(ItemGrouping::ByFullValue);

    // parse the json response with JsonDataAccess
    bb::data::JsonDataAccess ja;
    QVariant jsonva = ja.loadFromBuffer(info);

    QVariantMap map;
    map = jsonva.toMap().find("data")->toMap();
    //dm->insert(map);

    for (QMap<QString, QVariant>::iterator it = map.begin(); it != map.end(); it++) {
		QVariantMap tweet = it->toMap();
		dm->insert(tweet);
    }

    // set the data model to display
    list->setDataModel(dm);
    list->setVisible(true);

I guess I really didn't wuite understand this problem

Trusted Contributor
Posts: 140
Registered: ‎11-23-2010
My Device: Torch 9800
My Carrier: Rogers

Re: Getting to the data array in a JSON response

[ Edited ]

You need to paste an example of you json so I could check.

 

BTW Did you at least try to debug your stuff?? Does jsonva return something ? What about map?