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
Posts: 188
Registered: ‎04-07-2013
My Device: BlackBerry Z10
My Carrier: Telus

Parsing JSON Data

[ Edited ]

Hi all, I am trying to parse a data I got from a web service.

 

I have loaded it into a QVariant, how do I parse the JSON and display the first result? Thanks

 

void JSONParsingTest::replyReceived(QNetworkReply* networkReply)
{
	JsonDataAccess jsonDataAccess= new JsonDataAccess(this);
	QVariant jsonQVariant=jsonDataAccess.loadFromBuffer(networkReply->readAll());

	networkReply->deleteLater();
}

 

 

 

jayvdiyk.com
Developer
Posts: 16,851
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Parsing JSON Data

i would suggest to start with the JsonDataAccess API docs. There are also some sample projects that cover this.

you can always use qDebug to print the content of your QVariant, this should allow you to see what item you have (like QVariantList).
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 1,008
Registered: ‎12-12-2010
My Device: Passport (Red Limited Edition)
My Carrier: Mobile Vikings

Re: Parsing JSON Data

A little example:

 

QByteArray content = networkReply->readAll();
QString jsonString = QString::fromUtf8(content.data(), content.size());
bb::data::JsonDataAccess jda;
QVariant list = jda.loadFromBuffer(jsonString);
if(jda.hasError())
{
    bb::data::DataAccessError theError = jda.error();
    emit fetchError(1337, theError.errorMessage()); //My custom error signal
}
else
{
    QVariantMap result = list.value<QVariantMap>();
    pageTitle = result.value("title").toString();
    qDebug() << "Title: " << pageTitle;
    pageContents = result.value("content").toString();
    qDebug() << "Content: " << pageContents;
    QString iconUrl = result.value("favicon").toString();
    qDebug() << "Favicon: " << iconUrl;
    //Continue processing here
}

 

-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!
Developer
Posts: 230
Registered: ‎01-12-2013
My Device: z30
My Carrier: Mobile Vikings

Re: Parsing JSON Data

And if the result of the webservice is an array like this one

 

[
    {
        "firstname": "Alec",
        "lastname": "Saunders",
        "age": 25
    },
    {
        "firstname": "Thorsten",
        "lastname": "Heins",
        "age": 58
    },    
    {
        "firstname": "BlackBerry",
        "lastname": "Dev",
        "age": 25
    },
]

 

 

The code to handle the result is the following

 

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

JsonDataAccess jda;

QVariantList userList = jda.loadFromBuffer(result).toList();

Q_FOREACH(const QVariant &user, userList) {
    QVariantMap data = user.value<QVariantMap>();

    QString firstName = data.value("firstname").toString();
    QString lastName = data.value("lastname").toString();
    int age = data.value("age").toInt();

    // handle the rest
}

 

______________________________________________________
BB10-OAuth: GitHub
Developer
Posts: 109
Registered: ‎08-23-2012
My Device: Blackberry Z10
My Carrier: TATA DOCOMO

Re: Parsing JSON Data

First you need to confirm whether you are getting json array or json object.

For json object value is parsed as QvariantMap in key value pair but in case of json array, you will get QVariantList having   

all json objects as QVariantMap.

Dharmender Singh