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
Highlighted
Developer
Posts: 108
Registered: ‎09-21-2013
My Device: Blackberry Z10 STL100-1
My Carrier: Movistar
Accepted Solution

how do i parse a json list

Hi, I want it to parse a Json list of cities to use on a list in qml, I was using strings and properties to make them available on my qml but with just one city, and in labels, not a list, so how do I make it?

This is what I got till now

bb::data::JsonDataAccess ja;
QVariantMap all = (ja.loadFromBuffer(buffer).toMap());
QvariantList list (all["list"].toList());
QvariantMap list0 (list[o].toMap());
setName1

Y de ahí pondría hasta 4 nombres de ciudades que aparecen máximo, no se como manejarlo
Developer
Posts: 146
Registered: ‎07-03-2013
My Device: BB 9360
My Carrier: WIND

Re: how do i parse a json list

Can you please provide the snippet of raw JSON?

 

S.

Developer
Posts: 136
Registered: ‎01-25-2010
My Device: Z30, Z10, Q10, Dev Alpha B & Bold 9900
My Carrier: Bell

Re: how do i parse a json list

Here is a great tool to analyse your JSON data...

http://www.jsoneditoronline.org/

 

Also, I ran into a particular issue with one web service which I was consuming... The data returned was missing the leading [ and the ending ] therefore, the json parser would choke each time...

Since you are reading locally, you have control on your JSON data but if it's remotetly and you have no control, you could always do the following :

    // Retreive the data from the QNetworkReply
    QByteArray a = mReply->readAll();
    qDebug() << "Raw Data&colon; " << a;

    if (!(a.startsWith(']')))
      a.insert(0, '[');

    if (!(a.endsWith(']')))
      a.append(']');

 

Developer
Posts: 108
Registered: ‎09-21-2013
My Device: Blackberry Z10 STL100-1
My Carrier: Movistar

Re: how do i parse a json list

tnks this is my json, its from a web weather service

{"message":"accurate","cod":"200","count":4,"list":[{"id":2643743,"name":"London","coord":{"lon":-0.12574,"lat":51.50853},"main":{"temp":277.86,"pressure":1034,"humidity":75,"temp_min":276.15,"temp_max":279.15},"dt":1385328000,"wind":{"speed":3.6,"deg":360},"sys":{"country":"GB"},"clouds":{"all":40},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}]},{"id":6058560,"name":"London","coord":{"lon":-81.23304,"lat":42.983391},"main":{"temp":266.15,"pressure":1031,"humidity":67,"temp_min":266.15,"temp_max":266.15},"dt":1385326800,"wind":{"speed":5.1,"deg":280,"gust":8.2},"sys":{"country":"CA"},"snow":{"3h":0.375},"clouds":{"all":75},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}]},{"id":4517009,"name":"London","coord":{"lon":-83.44825,"lat":39.886452},"main":{"temp":268.83,"pressure":1033,"humidity":62,"temp_min":268.15,"temp_max":269.15},"dt":1385327100,"wind":{"speed":3.6,"deg":290},"sys":{"country":"US"},"clouds":{"all":75},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}]},{"id":4298960,"name":"London","coord":{"lon":-84.08326,"lat":37.128979},"main":{"temp":271.18,"pressure":1035,"humidity":42,"temp_min":270.15,"temp_max":272.15},"dt":1385327700,"wind":{"speed":3.6,"deg":40},"sys":{"country":"US"},"clouds":{"all":1},"weather":[{"id":800,"main":"Clear","description":"Sky is Clear","icon":"01d"}]}]}

so the maximum length of the list its 4 cities, but sometimes its 3 2 or 1 depending on the city

 

 

 

Developer
Posts: 108
Registered: ‎09-21-2013
My Device: Blackberry Z10 STL100-1
My Carrier: Movistar

Re: how do i parse a json list

This is what i have untill now, but is not working

 

void ExternalIP::getWeatherdataName(const QString screenName)
{
    QUrl url(QString("http://api.openweathermap.org/data/2.5/find?q=%1&mode=json&APPID=b03f0d5e79f09acc8cee487000486718")....
    QNetworkRequest request(url);

    QNetworkReply* reply = m_networkAccessManager->get(request);

    bool ok = connect(reply, SIGNAL(finished()), this, SLOT(onGetReplyList()));
    Q_ASSERT(ok);
    Q_UNUSED(ok);
}
void ExternalIP:Smiley SurprisednGetReplyList()
{
    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());

    QString response;
    if (reply){
        if (reply->error() == QNetworkReply::NoError) {
               const int available = reply->bytesAvailable();
               if (available > 0) {
               const QByteArray buffer(reply->readAll());

               bb::data&colon;:JsonDataAccess jda;
               const QVariant list = (jda.loadFromBuffer(buffer));

                       m_model->insertList(list.value<QVariantList>());
                   }
               }
    }
}

 

And in my qml

 

TextField {
            id: screenName
            hintText: qsTr("Enter City or Postal Code") + Retranslate.onLocaleOrLanguageChanged
            inputMode: TextFieldInputMode.Text
            input{
            submitKey: SubmitKey.Submit
            onSubmitted: {
                jsonlist.getWeatherdataName(screenName.text)
                }
}
    }
    ListView {
        dataModel: jsonlist.model
        listItemComponents: [
            ListItemComponent {
                type: "item"
                StandardListItem {
                    status: ListItemData.country
                    description: ListItemData.name
                    imageSpaceReserved: false
                }
            }
        ]
    }   
}

Developer
Posts: 108
Registered: ‎09-21-2013
My Device: Blackberry Z10 STL100-1
My Carrier: Movistar

Re: how do i parse a json list

It's finally work! This is the part that I change

const QVariant general = (ja.loadFromBuffer (buffer) .toMap());
QVariantList city = general["list"].toList();

foreach(QVariant var, city){
m_model->insert(var.toMap());

And then in my qml like this

ListItenComponent {
type: "item"
StandarListItem{
title: ListItemData.name
description: ListItemData.sys.country
}
}

Just one question, how do I erase or eliminate the grouping option, on the qml or in the c
Developer
Posts: 146
Registered: ‎07-03-2013
My Device: BB 9360
My Carrier: WIND

Re: how do i parse a json list

It'll be something like (in Qt)
M_model->setGrouping(ItemGrouping::None);
Developer
Posts: 108
Registered: ‎09-21-2013
My Device: Blackberry Z10 STL100-1
My Carrier: Movistar

Re: how do i parse a json list

Yeah thanks I find an example with that Smiley Happy
Thank you all