1

in my app when i hit an url i am getting a return data as an json array. it is as follows

{"Status":[{ "img_path": "http://xxxxxxxxxxxxxx.com/images/thumb140/1316145577.jpg", 
             "img_path": "http://xxxxxxxxxxxxxx.com/images/thumb140/1316146270.jpg", 
             "img_path": "http://xxxxxxxxxxxxxx.com/images/thumb140/1316146473.jpg", 
             "img_path": "http://xxxxxxxxxxxxxx.com/images/thumb140/1316147003.jpg" } ]} 

From the above result i am trying to parse out the urls and i am trying to store it in an array list. Following is my code

 try{
        JSONArray get_post_status = json.getJSONArray("Status");
        for (int i = 0; i < get_post_status.length(); i++) {
        JSONObject e = get_post_status.getJSONObject(i);
        if(e.equals("img_path"))
        {
            Get_post_image_array.add(e.getString("img_path"));
        }
        }

But in my arraylist i am getting only the last url from the result. How to get all the urls in the arraylist.

Pls help me.......

1
  • 1
    Did you know that you can use example.com as an example host name? example.com is a reserved domain name, as specified in RFC 2606. Commented Sep 19, 2011 at 15:30

2 Answers 2

3

The returned JSON is invalid.

Shortening the data, looking only at the structure:

{
    "Status":
        [
            {
                "img_path": "a", 
                "img_path": "b", 
                "img_path": "c", 
                "img_path": "d"
            }
        ]
}

We can see that the array (enclosed in []) only contains one element; the object enclosed by {}. This object has several instances of the same key, "img_path". This is not valid. Your parser apparently ends up with keeping only the last instance of it.

The JSON ought to look more like this:

{
    "Status":
        [
            "a", 
            "b", 
            "c", 
            "d"
        ]
}
Sign up to request clarification or add additional context in comments.

Comments

2

Your JSON is invalid.

{"Status": [ {"img_path": "blah, blah"}, {"img_path": "blah2, blah3"}]}

is what you want. Essentially, you were setting the same key in an object over and over, not creating a list of objects.

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.