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
nikunj_patel143
Posts: 22
Registered: ‎03-03-2011
My Device: Not Specified
Accepted Solution

Parse JSON

Hi

I am very new in JSON parsing......

How can i parseJSON like below.....please help


[{"id":48640,"end_date":"2011-11-01T00:00:00.000-0400","title":"FREE ACCESS TO OVER 10,000 NFB FILMS","image_url":"http://media.lavitrine.com/image/1288643351752.jpg","description":"FROM TUESDAY TO SUNDAY<br />NOON TO 9 PM<br /><br />Free access to over 10,000 NFB films<br /><br />Equipped with 21 single and double viewing stations, the CineRobotheque gives the public a chance to discover more than 10,000 NFB titles free-of-charge. To browse from one NFB film to another, simply slide your finger across the touch screen.<br /><br />FREE","sef_url":"http://www.lavitrine.com/activity/Free_access_to_over_10_000_NFB_films","last_minute":0,"start_date":"2010-11-01T00:00:00.000-0400","last_modification":"2010-11-02T15:35:26.000-0400"},{"id":37656,"end_date":"2012-03-01T00:00:00.000-0500","title":"KINO PASS","image_url":"http://media.lavitrine.com/image/1274378668928.jpg","description":"Kino’00 lance la Passe Kino ! Valeur 90 $ - Prix La Vitrine 50 $<br /><br />Les projections mensuelles de Kino proposent aux spectateurs, chaque 1er vendredi du mois, une variété de courts-métrages produits sans ou avec très peu de moyens. Comédie, drame, fantastique, documentaire, film d’art ou OFNI (objet filmé non identifiable), tous les genres sont au rendez-vous!<br /><br />PROCHAINE SOIRÉE : 3 SEPTEMBRE 2010 À 20 h 30 <br /><br />La Passe Kino  permet d’assister  à toutes ces projections mensuelles et voir des courts-métrages inédits. En bonus, le DVD Planète Kino avec le meilleur de Kino de l’année est offert !<br /><br />Les projections mensuelles se déroulent dans une chaleureuse ambiance de cabaret, les réalisateurs-vedettes viennent à tour de rôle présenter leur film en personne. Le public est ensuite invité à aller à leur rencontre durant l’entracte et à la fin de la soirée.<br /><br />La projection mensuelle est un lieu de motivation, un catalyseur pour les artistes. Afin de stimuler l’effet d’entraînement, l’animateur de la soirée invite les réalisateurs à annoncer publiquement la date de présentation de leur prochain film.<br /><br />Au sujet de Kino…<br />Kino est un mouvement créé à Montréal en 1999 dans le but d’encourager la production et la diffusion d’œuvres cinématographiques indépendantes et originales. Aujourd’hui, on retrouve plus de 60 cellules Kino dans 14 pays sur 4 continents.","sef_url":"http://www.lavitrine.com/activity/KinoPass","last_minute":0,"start_date":"2011-03-01T00:00:00.000-0500","last_modification":"2010-08-12T09:51:32.000-0400"}]

Please use plain text.
Developer
YamilBracho
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: Parse JSON

Something like :

 

Please use plain text.
Developer
YamilBracho
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: Parse JSON

JSONArray arr = new JSONArray(jsonString);
for (int i=0, len = arr.length; i < len; i++) {
   JSONObject obj = arr.getJSONObject(i);
   System.out.println("id=" + obj.getString("id");
   System.out.println("end_date=" + obj.getString("end_date");
   System.out.println("title=" + obj.getString("title");
   System.out.println("image_url=" + obj.getString("image_url");
   System.out.println("description=" + obj.getString("description");
   System.out.println("sef_url=" + obj.getString("sef_url");
   System.out.println("last_minute=" + obj.getString("last_minute");
   System.out.println("start_date=" + obj.getString("start_date");
   System.out.println("last_modification=" + obj.getString("last_modification");
 }  

 

Something like :

 

Please use plain text.
Contributor
nikunj_patel143
Posts: 22
Registered: ‎03-03-2011
My Device: Not Specified

Re: Parse JSON

Thank you man.......................

its working awesome.........................

 

Please use plain text.
New Contributor
TessyJose
Posts: 8
Registered: ‎11-17-2011
My Device: developer
My Carrier: developer

Re: Parse JSON

hi ,

i tried your example and many others available all over google , but just not able to parse the json response :smileysad:

 

this is what i receive from my servlet and is stored in a string variable :

 

{"MyBean":{"username":"john","jsonReadyString":"hello john !!"}}

 

no matter what code i try , it goes to the catch block and thr is "no stacktrace" available . i want to parse this and put it into a pojo.  pls help :smileysad: i am at my wits end

 

Please use plain text.
Developer
YamilBracho
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: Parse JSON

try :

String jsonString = "{\"MyBean\":{"\username\":\"john\",\"jsonReadyString\":\"hello john !!\"}}";
JSONObject myBean = new JSONObject(jsonString);
String username = myBean.getString("username");
String jsonReadyString = myBean.getString("jsonReadyString");

 

Please use plain text.
New Contributor
TessyJose
Posts: 8
Registered: ‎11-17-2011
My Device: developer
My Carrier: developer

Re: Parse JSON

no luck :smileysad: still goes to the catch block ... no stack trace . any idea why this is happening ?

Please use plain text.
New Contributor
TessyJose
Posts: 8
Registered: ‎11-17-2011
My Device: developer
My Carrier: developer

Re: Parse JSON

i managed to figure out a dirty fix

 

 

try

{

JSONObject myBean =newJSONObject(content);

String alias = myBean.getString("MyBean");

 

System.out.println("alias.........................."+alias);  

  //output-    alias......................{"username":"john","jsonReadyString":"john hello !!"}

 

JSONObject myBean2=newJSONObject(alias);

String username=myBean2.getString("username");

System.out.println("username.........................."+username);

//output-         username..........................john

 

String jsonReadyString=myBean2.getString("jsonReadyString");

System.out.println("jsonReadyString.........................."+jsonReadyString);

//output-         jsonreadystring..........................john hello !!

}

catch(JSONException e) {

 

e.printStackTrace();

}

 

But this code would it thr were more rows of username and jasonreadystring. Any advice on iterating thru arrays in this situation?

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Parse JSON

Sorry not looked at you JSON processing,, but the reason you are not getting a stack trace is that you are not catching Throwable.  To get a stack tracde you have to have code like this:

 

try {

} catch (Throwable t) {

t.printStacktrace();

...

}

 

in this case, since it appears your exception was a JSONExceptin anyway, I would have thought printing the exception (e.toSting()) would have told you what the problem is,

 

Your 'work around' of using a JSONObject to process the contents of another Object seems to me to be exactly what is required here.  If you look at the various parsing options like this, you need to invoke the appropriate 'matching' one to get your data.  This is one of the differences between XML and JSON, it seems you do have to know what you are going to get to parse a JSON response.    Or probably I am just missing something.... 

Please use plain text.
Contributor
christopherbecker
Posts: 28
Registered: ‎11-13-2012
My Device: none
My Carrier: 27768965251

Re: Parse JSON

Hi, your code works, it gets the amount of json fields and all that, but when I try to display the content as how you are doing it, I get this error.

 

Do you perhaps know why?

error104.gif

Please use plain text.