1

My JsonString:

{"Data":{"pics":{"24":{"ID":"24","LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/010.jpg"},"23":{"ID":"23","LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/009.jpg"},"22":{"ID":"22","LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/008.jpg"},"21":{"ID":"21","LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/007.jpg"},"20":{"ID":"20","LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/006.jpg"},"19":{"ID":"19","LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/005.jpg"},"18":{"ID":"18","LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/004.jpg"},"17":{"ID":"17","LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/003.jpg"},"16":{"ID":"16","LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/002.jpg"},"15":{"ID":"15","LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/001.jpg"},"14":{"ID":"14","LINK":"http:\/\/orf.at\/mojo\/1_1\/storyserver\/\/news\/images\/logo.newsorfon.png"}}},"Message":null,"Code":200}

I want to parse it to get the id and the link into my treemap.

My Code:

JSONObject parse = new JSONObject(msg.obj.toString());
 JSONObject data = parse.getJSONObject("Data");
 JSONArray arr = data.getJSONArray("pics");
 for (int i = 0; i<arr.length(); i++) {
 JSONObject obj = arr.getJSONObject(i);
 list.put(Integer.parseInt(obj.getString("ID")), obj.getString("LINK"));
 Log.e("FunPics",obj.getString("ID")+ " " + obj.getString("LINK"));
 }

Error:

11-21 11:32:21.005: W/System.err(393): org.json.JSONException: JSONObject["pics"] is not a JSONArray.
11-21 11:32:21.015: W/System.err(393): at org.json.JSONObject.getJSONArray(JSONObject.java:366)

Qwerky
18.5k6 gold badges48 silver badges85 bronze badges
asked Nov 21, 2011 at 11:34
1
  • put the json string in json array i mean put "msg.obj.toString()" into getJSONArray() instead of "pics" and then using for loop get "pics" as a jsonobject...!! and One "more" thing is the JSONString coming from webservice is wrong it must come in [{Data:......}] format Commented Nov 21, 2011 at 12:24

3 Answers 3

2

pics is not an array, it's an object, array is indicated by being in [ and ] (as specified here):

{
"Data":{
 "pics":{
 "24":{
 "ID":"24",
 "LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/010.jpg"
 },
 "23":{
 "ID":"23",
 "LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/009.jpg"
 },
 }
},
....
"Message":null,
"Code":200
}
answered Nov 21, 2011 at 11:39
Sign up to request clarification or add additional context in comments.

Comments

1

"pics" is a jsonObject not a jsonArray so write data.getJSONObject("pics") in place of data.getJSONArray("pics");

answered Nov 21, 2011 at 11:40

Comments

1

pics key has jsonObject as value not a jsonArray.

JSONObject picsJson = data.getJSONObject("pics");

you have to iterate through all the keys and get each information of picture. use keys iterator, check the link below,

http://developer.android.com/reference/org/json/JSONObject.html

Iterator keys = picsJson.keys();
while(keys.hasNext()) {
bharath
14.6k16 gold badges61 silver badges95 bronze badges
answered Nov 21, 2011 at 11:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.