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

Java Development

Reply
Contributor
christopherbecker
Posts: 28
Registered: ‎11-13-2012
My Device: none
Accepted Solution

JSON Results Giving Error

[ Edited ]

I'm currently getting json data from my web service, This part is a success, it's working perfectly.

Secondly, I run this code to process the JSON.

 

JSONArray arr = new JSONArray(jsonList);
for (int i=0, len1 = arr.length(); i < len1; i++) {
   JSONObject obj = arr.getJSONObject(i);
   RichTextField showJson = new RichTextField("Username: " + obj.getString("Username"));
   add(showJson);
   add(new RichTextField("--------------"));
}

 

But I get an error

error104.gif

 

But when I comment out:

JSONObject obj = arr.getJSONObject(i);
RichTextField showJson = new RichTextField("Username: " + obj.getString("Username"));

 

Then it works perfectly by going through each record in the JSON.

But then it obviously only displays this

add(new RichTextField("--------------"));

4 times, as many as there are in the JSON

 

Does anyone perhaps know why, i'm getting the ERROR, when I display the JSON records.

I got the code from the thread, where they state that it works.

http://supportforums.blackberry.com/t5/Java-Development/Parse-JSON/td-p/1314943

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: JSON Results Giving Error

Code looks fine to me.

 

Run this in debug mode.  Then when it breaks, it will stop at the error and you look at the stack trace to find out exactly which statement caused the problem.  Let us know.

 

Also how have you got JSON support in this?  Is this the built in JSON?

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: JSON Results Giving Error

json parsing should not be done on the event thread, btw.
----------------------------------------------------------
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
Contributor
christopherbecker
Posts: 28
Registered: ‎11-13-2012
My Device: none

Re: JSON Results Giving Error

Yeah the built in one.

 

But I got it working! whoo hoo!

I changes this code:

RichTextField showJson = new RichTextField("Username: " + obj.getString("Username"));

to:

StringBuffer buildBubbles = new StringBuffer().append(obj.getString("Username"));

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: JSON Results Giving Error

OK, can you mark this as solved?  Instructions in the Help link near the top of the screen if you need them. 

 

Just repeating Simon's point, you need to make sure that you download and parse the JSON from a Background Thread, i.e. a Thread that you create and then

<thread>.start();

 

This of course causes a problem - what does the user do while waiting for this download?  Search the forum for answers to this, look for things like progressbar or 'PleaseWait', or loading.