02-16-2011 10:37 AM
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)
Solved! Go to Solution.
02-16-2011 10:43 AM
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.
02-16-2011 10:46 AM
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/ja
02-16-2011 12:39 PM
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
02-16-2011 03:32 PM
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.
02-17-2011 12:34 PM
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 } ] }
02-17-2011 12:41 PM
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.
02-17-2011 12:54 PM - edited 02-17-2011 02:21 PM
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.
03-26-2011 05:00 AM
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......
03-26-2011 09:33 AM