2

I have an array of key-value pairs like this:

{
  "320x240":"http:\/\/static.example.com\/media\/content\/2012\/Jul\/mercedes-benz-a-klasse-red-t_320x240.jpg",
  "300x225":"http:\/\/static.zigwheels.com\/media\/content\/2012\/Jul\/mercedes-benz-a-klasse-red-t_300x225.jpg",
  "200x150":"http:\/\/static.zigwheels.com\/media\/content\/2012\/Jul\/mercedes-benz-a-klasse-red-t_200x150.jpg"
}

What I'm doing currently is this:

   try {

      images_object = new JSONObject(imageList);//imageList is a String of the above array //of key value pairs

            Iterator<?> keys = images_object.keys();
            String string_images = "";
           if(keys.hasNext()) {
               String key = (String)keys.next();
                String value = (String)images_object.get(key);
                string_images = "[" + value;  
           }
            while( keys.hasNext() ){
                String key = (String)keys.next();
                String value = (String)images_object.get(key);
                string_images = string_images + "," + value;

            }
            string_images = string_images + "]";
            String encoded_json_string = JSONObject.quote(string_images);
            images = new JSONArray(encoded_json_string);//images is of type JSONArray but it is null
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

But, images, is NULL. Why's that? What am I missing?

3
  • Because you're not creating valid JSON. string_images will in no way resemble valid JSON which requires strings to be quoted. Commented May 9, 2013 at 5:28
  • Could you tell me what is wrong? I also tried removing the square brackets. Commented May 9, 2013 at 5:31
  • @Namratha What is your problem with the below answer? Have you tried that? Commented May 9, 2013 at 7:07

1 Answer 1

4

you can get all values from current JSONObject in JSONArray as:

Iterator<String> keys = images_object.keys();
JSONArray images = new JSONArray();
while( keys.hasNext() ){
    String key = keys.next();
    String value = images_object.optString(key);
    //add value to JSONArray from JSONObject
    images.put(value);
}

EDIT

Simplified solution is get the keys with images_object.names() and you can pass JSONArray of keys to toJSONArray method to get the value with respect to keys in JSONArray

JSONArray keys=images_object.names();
JSONArray values=images_object.toJSONArray(keys);

To Summing up simplified solution is:

JSONArray images=images_object.toJSONArray(images_object.names());
Sign up to request clarification or add additional context in comments.

13 Comments

The method add(String) is undefined for the type JSONArray.
@Namratha : then you can use JSONArray.put . see my edit answer
@Pragnani : hi, but i think requirement is to get value associated with name or keys in JSONArray
@ρяσѕρєяK Please include above comment in answer so that it will help others..It will do the things in one line
@ρяσѕρєяK Above solution will give values in JSONArray format not keys
|

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.