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. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Developer
Kiran_Snist
Posts: 174
Registered: ‎01-25-2011
Accepted Solution

Json Object parsing

I have been asked to parse and use the data, thats been sent from the server in the form of Objects.

 

Till now what i have done is, i used to  a simple string from the server and i used to take it in to a String Buffer and convert into string using .toString() function. Now as i am supposed to get a Json Object, i want to know how could one take it into String Buffer and convert it into a usable format, may be into an array of Strings. 

 

So. i request u guys me help me out in this issue and make my app dev progress little bit quicker and efficient

 

Thanks in Advance!!

 

Regards

 

Kiran

(Snist)

Please use plain text.
Developer
simon_hain
Posts: 13,754
Registered: ‎07-29-2008
My Carrier: O2 Germany

Re: Json Object parsing

 

the json.me package is part of OS 6, in prior OS versions add it to your project.

 

creating a json object is quite simple:

 

JSONObject jsonObject = new JSONObject(new String(IOUtilities.streamToBytes(httpConnection.openInputStream())));

 

the difficult part is ensuring that everything is in the expected format and enumerationg through all the lists etc.

 

----------------------------------------------------------
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
Please use plain text.
Developer
indusBULL
Posts: 207
Registered: ‎09-10-2010

Re: Json Object parsing

Kiran,

 

Have you googled or searched on this forum? You would have got lot of useful results. Let me do quick search for you.

 

http://java.sun.com/developer/technicalArticles/javame/json-me/    

Please use plain text.
Developer
Kiran_Snist
Posts: 174
Registered: ‎01-25-2011

Re: Json Object parsing

IndusBull,

 

I have had gone through ur URL.. which was thorougly useful. 

 

The server side has given me some links to use it them in my code to get resources from the server.

Now that i paste the codes in the browser 2 chk de availability of the links, i m getting the text something like "Json Text Example" that  is mentioned in that URL  as Listing 2.

 

 

Now my question is, is that the o/p of a toJson function present in the URL???(outer.toString) as Listing 4.

 

If thats the case, can i directly make use of the fromJson function present in the URL( not exactly de same though)  as Listing 5, by implementing the class in client side , that is existing in the server side??

 

Please so reply, i need it Urgently... 

 

Awaiting!!

 

 

Regards

Kiran

Please use plain text.
Developer
indusBULL
Posts: 207
Registered: ‎09-10-2010

Re: Json Object parsing

Hi Kiran

 

I m not sure if I clearly understood your question. Do you want to decode/parse the json string (like listing 2) that you got from the server at client side? If yes then you have to write method similar to fromJSON to parse JSON string. You can omit toJSON method if you are not going to send any json object to server.

 

Please use plain text.
Developer
Kiran_Snist
Posts: 174
Registered: ‎01-25-2011

Re: Json Object parsing

hi IndusBull, ThanX for ur reply

 

Now i got another doubt. sy, dat you have an Array of Objets like the one shown below, then how could i access the inner JSON objects and how can i assume the number of objects retruned frm the server before hand... Just lemme know the details!!

Thaks!!

 { "accounting" : [   // accounting is an array in employees.
                                   
{ "firstName" : "John",  // First
                                     
"lastName"  : "Doe",
                                     
"age"       : 23 },
                                   
                                   
{ "firstName" : "Mary",  // Second
                                     
"lastName"  : "Smith",
                                     
"age"       : 32 } ] }
Please use plain text.
Developer
jprofitt
Posts: 604
Registered: ‎12-27-2010

Re: Json Object parsing

 

JSONArray accountingArray = myMainJsonObject.getJSONArray("accounting");

for(int i=0;i<accountingArray.length();i++) {
        JSONObject employee = accountingArray.getJSONObject(i);
        //do stuff with employee
}

 

 

myMainJsonObject is whatever JSONObject you're storing that data in.

Please use plain text.
Developer
indusBULL
Posts: 207
Registered: ‎09-10-2010

Re: Json Object parsing

[ Edited ]

you can do something like this 

 

JSONArray array = jsonObj.getJSONArray("arrayKey");
int noObj = array.length();

 to get number array size and then u can have for loop to iterate through all objects inside the array. 

 

Edit: Ohh..I didn't notice above post. Prob took a long time to write response.  

 

 

Please use plain text.
Contributor
gaurav2k11
Posts: 20
Registered: ‎03-04-2011
My Carrier: application developer

Re: Json Object parsing

hello every one ...

this is my fisrt time that i m doing parsing please help me..

i m getting response from server in th form of 

{“S”:”12345.00”,

”P”:”34566.33”,

”B”:”2600”,

”Drs”:”-2340”,

”Crrs”:”54040”,

”Prs”:”-34343.23”

}

 now i have to parse that and print on the page with therir value ......

please give some idea how to do that ...

waiting for reply.....

 

thanx in advance......

Please use plain text.
Developer
jprofitt
Posts: 604
Registered: ‎12-27-2010

Re: Json Object parsing

JSONObject response = new JSONObject(<what you got from server>);
String s = response.getString("S");
String p = response.getString("P");
//etc.
Or you can use getFloat()/getInt() if you don't want them as Strings
Please use plain text.