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
New Developer
Posts: 26
Registered: ‎05-09-2013
My Device: Blackberry Z10
Accepted Solution

How to Parse JSON Data from server ??

I am using an API and I get responces in JSON format. I need help in parsing the recieved JSON data.

 

This is the data I recieve..

{"code":200,"lang":"en-en","text":["hello"]}"

 

I need to extract the value of text (i.e :hello must be extracted)

Please help me out with tis issue!!

 

Thanks

Developer
Posts: 540
Registered: ‎08-31-2013
My Device: Q10, Z10LE, Nokia N950, DAB....

Re: How to Parse JSON Data from server ??

[ Edited ]

In C++ JsonDataAccess is for you

http://developer.blackberry.com/native/reference/cascades/bb__data__jsondataaccess.html

 

In QML:

var json = JSON.parse(response)

Now you can access to the property like this

CODE = json.code

LANG = json.lang

Etc...



If helped give a like. If your problem was solved mark the post as "Solution"
Portfolio: BlackBerry Nokia
Social: Twitter | Facebook
About Me / Blog DevPDA | Blog
New Developer
Posts: 26
Registered: ‎05-09-2013
My Device: Blackberry Z10

Re: How to Parse JSON Data from server ??

Thanks for quick response! Will give it a try!!

Developer
Posts: 540
Registered: ‎08-31-2013
My Device: Q10, Z10LE, Nokia N950, DAB....

Re: How to Parse JSON Data from server ??

You're welcome :smileywink:



If helped give a like. If your problem was solved mark the post as "Solution"
Portfolio: BlackBerry Nokia
Social: Twitter | Facebook
About Me / Blog DevPDA | Blog
New Developer
Posts: 26
Registered: ‎05-09-2013
My Device: Blackberry Z10

Re: How to Parse JSON Data from server ??

Hi,

 I tried this code..

QString jsonResponse = reply->readAll();

JsonDataAccess jda;

QVariantMap qmap = jda.loadFromBuffer(jsonResponse).value<QVariantMap>();

qDebug()<<qmap["code"].toString();

 

I got the answer for "code" as 200

but when i tried qDebug()<<qmap["text"].toString() , I am getting a null string ("")

 

Developer
Posts: 1,523
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: How to Parse JSON Data from server ??

Hi,
Because it's an array.

QVariantList lst = qmap["text"].toList();
if (lst.size() > 0)
{
qDebug() << lst[0].toString();
}

Andrey Fidrya, @zmeyc on twitter
New Developer
Posts: 26
Registered: ‎05-09-2013
My Device: Blackberry Z10

Re: How to Parse JSON Data from server ??

:Clap: Got it !!

 

Thanks !!

Developer
Posts: 781
Registered: ‎05-29-2012
My Device: see later

Re: How to Parse JSON Data from server ??

is this also from a web service call? or the json file is present on ur app?

 

m_soap.setAction("http://tempuri.org/GetCountries");
        m_soap.setHost("168.187.121.70");

        QtSoapMessage request;
        request.setMethod(QtSoapQName("GetCountries", "http://168.187.121.70/WSJAWW/"));


        // Submit the method request to the web service.
        m_soap.submitRequest(request, "/WSJAWW/service.asmx");

 

 

how can i parse that?  const QtSoapMessage& response = m_soap.getResponse(); ...and then what? if i get xml or json format?

or should i do shtg else?