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
Contributor
lichienmine
Posts: 36
Registered: ‎08-15-2013
My Device: Blackberry 10
Accepted Solution

Problem parsing JSON Array to ListView

I need some help to parsing JSON Array to ListView.

I have no problem to parse this JSON 

[
    {
        "id": "123",
        "name": "KFC",
        "contact": {
            "phone": "+1175757",
            "twitter": "kfc"
        }
    }

 to this qml :

 

import bb.cascades 1.0
import bb.data 1.0
Page {
    content: ListView {
        id: listView
        dataModel: dataModel
        listItemComponents: [
            ListItemComponent {
                type: "item"
                Container {
                    verticalAlignment: VerticalAlignment.Center
                    Container {
                        horizontalAlignment: HorizontalAlignment.Fill
                        verticalAlignment: VerticalAlignment.Center
                        layout: DockLayout {
                        
                        }
                        Container {
                            horizontalAlignment: HorizontalAlignment.Fill
                            verticalAlignment: VerticalAlignment.Center
                            topPadding: 20
                            layout: StackLayout {
                                orientation: LayoutOrientation.LeftToRight
                            }
                            Container {
                                verticalAlignment: VerticalAlignment.Center
                                leftMargin: 30
                                Label {
                                    bottomMargin: 0
                                    bottomPadding: 0
                                    text: ListItemData.name
                                    verticalAlignment: VerticalAlignment.Center
                                    textStyle {
                                        base: SystemDefaults.TextStyles.TitleText
                                        fontWeight: FontWeight.Normal
                                    }
                                }
                            }
                        }
                    }
                    Divider {
                    }
                }
            }
        ]
    }
    attachedObjects: [
        GroupDataModel {
            id: dataModel
        },
        DataSource {
            id: dataSource
            source: "myjson.json"
            onDataLoaded: {
                dataModel.insertList(data)
            }
        }
    ]
    onCreationCompleted: {
        dataSource.load(); 
    }
}

 but i don't know how to work with this type of JSON :

{
    "meta": {
        "code": 200
    },
    "response": {
        "myplace": [
            {
                "id": "123",
                "name": "KFC",
                "contact": {
                    "phone": "+1657465",
                    "twitter": "kfc"
                }
            }
        ]
    }
}

 can anybody help please !

Contributor
casantos
Posts: 33
Registered: ‎01-13-2013
My Device: BlackBerry Z10, Q10

Re: Problem parsing JSON Array to ListView

The problem is that DataSource will convert your JSON to a QVariantMap, not to a QVariantList, as required by GroupDataModel::insertList().  Try this:

 

        DataSource {
            id: dataSource
            source: "myjson.json"
            onDataLoaded: {
                dataModel.insertList(data.response.myplace)
            }
        }

 

 

 

Contributor
lichienmine
Posts: 36
Registered: ‎08-15-2013
My Device: Blackberry 10

Re: Problem parsing JSON Array to ListView

thanks, its solve my problem