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

Problems parsing json from qbytearray

help with parsing this json:

 

{"coord":{"lon":-97.87,"lat":22.26},"sys":{"country":"MX","sunrise":1383827966,"sunset":1383868260},"weather":[{"id":800,"main":"Clear","description":"Sky is Clear","icon":"01n"}],"base":"cmc stations","main":{"temp":26.928,"temp_min":26.928,"temp_max":26.928,"pressure":1026.4,"sea_level":1027.46,"grnd_level":1026.4,"humidity":85},"wind":{"speed":4.46,"deg":77.0002},"clouds":{"all":0},"dt":1383786373,"id":3530594,"name":"Ciudad Madero","cod":200}

I got it in a QByteArray as a buffer but i dont know how to mapping it to take every part of it and use it on my labels later, i got this until now, and got some of them but in just one label

 

 const QByteArray buffer(reply->readAll());
                bool ok;
               

                bb::data::JsonDataAccess ja;
                const QVariant jsonva = ja.loadFromBuffer(buffer);
                const QMap<QString, QVariant> externalip = jsonva.toMap();

                foreach (const QVariant &value, externalip) {
                    response += value.toString();
                }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: Problems parsing json from qbytearray

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

Re: Problems parsing json from qbytearray

Tnks foru quick anwser, ive been cheking on the link you gave me, but my json file is a little more complex, so i dont know if im doing it right, but, this is what i got

 

const QByteArray buffer(reply->readAll());

                bb::data&colon;:JsonDataAccess ja;
                const QVariant jsonva = ja.loadFromBuffer(buffer);
                const QVariantMap sys, externalip = jsonva.toMap();
                const QString country = sys["country"].toString();
                const QString sunrise = sys["sunrise"].toString();
                const QString sunset = sys["sunset"].toString();
                const QVariantList weather, externalip =


Is it correct? and i dont know about the last line

any help would be appreciate

Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

Re: Problems parsing json from qbytearray

Tricky part is that weather is an array...

 

    QString fred = "{\"coord\":{\"lon\":-97.87,\"lat\":22.26},\"sys\":{\"country\":\"MX\",\"sunrise\":1383827966,\"sunset\":1383868260},\"weather\":[{\"id\":800,\"main\":\"Clear\",\"description\":\"Sky is Clear\",\"icon\":\"01n\"}],\"base\":\"cmc stations\",\"main\":{\"temp\":26.928,\"temp_min\":26.928,\"temp_max\":26.928,\"pressure\":1026.4,\"sea_level\":1027.46,\"grnd_level\":1026.4,\"humidity\":85},\"wind\":{\"speed\":4.46,\"deg\":77.0002},\"clouds\":{\"all\":0},\"dt\":1383786373,\"id\":3530594,\"name\":\"Ciudad Madero\",\"cod\":200}";

    JsonDataAccess jad;
    QVariantMap map(jad.loadFromBuffer(fred).toMap());
    QVariantMap sys(map["sys"].toMap());
    qDebug() << sys["country"].toString();
    QVariantList weather(map["weather"].toList());
    QVariantMap weather0(weather[0].toMap());
    qDebug() << weather0["main"].toString() << weather0["description"].toString();

 

--
Rob is no longer associated with BlackBerry.
Developer
edusal100
Posts: 108
Registered: ‎09-21-2013
My Device: Blackberry Z10 STL100-1

Re: Problems parsing json from qbytearray

Tnks I'm gonna check that way, but I got a slot and signal that I use to get the data on my qml, I don't know how to work wit it, so I could pull every value string on a label, like main, description and so on

Q_OBJECT
Public:
ExternalIP (QObject* parent = 0);

Public Q_SLOTS:
void getWeatherdata ();

Q_SIGNALS:
void complete (const QString &temp);

And on my main.qml

jsonweatherdata.getweatherdata();

ExternalIP{
Id: jsonweather
On complete : {
weatherDataTemperature.text = temp;

That's my label
Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

Re: Problems parsing json from qbytearray

The code above can help you do that...

 

Instead of qDebug(), set a property...

 

QML having JavaScript makes JSON manipulation easier from inside the QML though...

 

If you just have a handler for the JSON changing, it can extract the values and set the controls...

--
Rob is no longer associated with BlackBerry.
Developer
edusal100
Posts: 108
Registered: ‎09-21-2013
My Device: Blackberry Z10 STL100-1

Re: Problems parsing json from qbytearray

Can you show me an example? Just for the first part, or the way it could be done in Java script?
Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

Re: Problems parsing json from qbytearray

Consider country...

 

In the header, you would have a property:

 

public:
Q_PROPERTY (QString country READ country WRITE setCountry NOTIFY countryChanged);
Q_INVOKABLE QString country() {
return _country;
}
Q_SLOT void setCountry(QString c) {
  if (c!=_country) {
    _country = c;
    emity countryChanged(c);
  }
}

signals:
  void countryChanged(QString);

private:
QString _country

 In thebody - as you'd received new JSON, 

    JsonDataAccess jad;
    QVariantMap map(jad.loadFromBuffer(fred).toMap());
    QVariantMap sys(map["sys"].toMap());
    setCountry(sys["country"].toString());

 

The QML would need to be bound to the country property...

 

--
Rob is no longer associated with BlackBerry.
Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

Re: Problems parsing json from qbytearray

For Javascript, you'd have code like this in your QML...

 

Label {
  id: countryLabel
}

function handleJSON(j) {
  var val = JSON.parse(j);
  countryLabel.text = val["sys"]["country"];
}

 

--
Rob is no longer associated with BlackBerry.
Developer
edusal100
Posts: 108
Registered: ‎09-21-2013
My Device: Blackberry Z10 STL100-1

Re: Problems parsing json from qbytearray

So I use properties instead of the handler I have or what do you mean with that the handler can extract the values and set the controls, sorry for so many questions