Reputation: 1312
i have a json response but is this standard format?
[{"code":"123","rate":"0.1","title":"sss","subtitle":"mmm"},{"code":"456","rate":"0.1","title":"bbb","subtitle":"uuu"}]
and how can i pars it in android?
new JSONObject(result.toString())
throw below exception!
06-07 11:45:54.943: W/System.err(10310): at org.json.JSON.typeMismatch(JSON.java:111)
06-07 11:45:54.943: W/System.err(10310): at org.json.JSONObject.<init>(JSONObject.java:158)
06-07 11:45:54.943: W/System.err(10310): at org.json.JSONObject.<init>(JSONObject.java:171)
Upvotes: 1
Views: 4555
Reputation: 56
First assign your json array like this
JSONArray jsar=new JSONArray(result);
then parse objects like this
for(int i=0;i<jsar.length;i++)
{
JSONObject jsobj=new JSONObject(jsar[i]);
String strcode=jsobj.get("code");
String strrate=jsobj.get("rate");
String strtitle=jsobj.get("title");
String strsubtitle=jsobj.get("subtitle");
}
hope this will help you,.
Upvotes: 1
Reputation: 10076
this is JSONarray
[{"code":"123","rate":"0.1","title":"sss","subtitle":"mmm"},{"code":"456","rate":"0.1","title":"bbb","subtitle":"uuu"}]
try to parse
new JSONArray(result.toString())
Upvotes: 3
Reputation: 1074595
i have a json response but is this standard format?
It would be without the <
at the beginning and the >
at the end. So:
new JSONObject(result.toString().substring(1, result.length() - 2));
or more likely:
new JSONArray(result.toString().substring(1, result.length() - 2));
since the top level of it is an array.
Since you've removed the <
and >
you had originally:
Since the top level of it is an array, you probably want JSONArray
, not JSONObject
:
new JSONArray(result.toString());
Upvotes: 3