09-15-2011 02:12 PM
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-0
Solved! Go to Solution.
09-15-2011 04:18 PM
Something like :
09-15-2011 04:19 PM
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 :
09-16-2011 09:30 AM
Thank you man.......................
its working awesome.........................
11-17-2011 08:26 AM
hi ,
i tried your example and many others available all over google , but just not able to parse the json response ![]()
this is what i receive from my servlet and is stored in a string variable :
{"MyBean":{"username":"john","jsonReadyString":"he
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
i am at my wits end
11-17-2011 09:03 AM
try :
String jsonString = "{\"MyBean\":{"\username\":\"john\",\"jsonReadyStr ing\":\"hello john !!\"}}";
JSONObject myBean = new JSONObject(jsonString);
String username = myBean.getString("username");
String jsonReadyString = myBean.getString("jsonReadyString");
11-18-2011 01:01 AM
no luck
still goes to the catch block ... no stack trace . any idea why this is happening ?
11-18-2011 05:43 AM
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","jso
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.................
}
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?
11-18-2011 05:53 AM
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....
11-16-2012 04:57 AM
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?