1

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)
2
  • yes it's valid you can paste your Json in jsonlint.com for check validation Commented Jun 7, 2014 at 7:27
  • 1
    your json is an Array not an object so you need new JSONArray(result.toString()); Commented Jun 7, 2014 at 7:28

3 Answers 3

3

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());
Sign up to request clarification or add additional context in comments.

4 Comments

you are correct.but the problem is not < and >;thanks
@Mimad: In what way is not those? Those make it invalid JSON, so you can't parse it. Without them, it is valid JSON, and you can parse it. (Also: You probably want new JSONArray rather than new JSONObject, as it's an array, not an object. I've added that.)
it s my fault in logging!:D
@Mimad: Ah, okay. So it's just the JSONObject vs. JSONArray thing.
3

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())

Comments

1

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,.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.