Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have JSON array like below format

{
"get": [],
"post": {
    "_event": "get_buyers_guide_detail",
    "_deviceID": "490154203237518",
    "type": "cars",
    "model_id": "1007"
},
"cars": [
    {
        "ID": 6119,
        "post_title": "COROLLA",
        "interior_images": [
            "http://.....e8664969d20654bf2a58f1b26de70d2.jpg",
            "http://........02/3bdfae250e3ce14514fe8f2f9dc3e58f.jpg",
            "http://........1d14a554a2ed78d1659463ec.jpg"
        ],

    }
]
}

I have create JSONArray for this:

JSONArray Array = entries.getJSONArray("cars");
for (int i = 0; i < Array.length(); i++) {
   JSONObject detailObject = Array.getJSONObject(i);
   String ID = detailObject.getString("ID");
   String title = detailObject.getString("post_title");
   JSONArray galleryArray = detailObject.getJSONArray("interior_images");
   for(int j=0; j<galleryArray.length(); j++){
      // What to do here ????   
   }
}

now i am confused with this format of JSON data. There is no JSONObject to get the values. How to insert these string on Array please help me find out the solution

share|improve this question
4  
Your JSON is not valid. –  Prince Jul 4 '14 at 4:38
    
@Prince im only showing here my important part –  user3751280 Jul 4 '14 at 4:39
2  
show your complete JSON –  Ahmed Nawaz Jul 4 '14 at 4:42
1  
@user3751280 keys are very important in JSON. You are missing them. –  Lazy Ninja Jul 4 '14 at 4:48
1  
possible duplicate of JSON Array of strings (no objects), extracting data –  eski Jul 4 '14 at 4:53

5 Answers 5

up vote 1 down vote accepted

Try this,

galleryArray.getString(j);

this should get the string at position j.

share|improve this answer

galleryArray.getString(index);

This function will return String object of corresponding index

share|improve this answer

try this

 ArrayList<String> stringArray = new ArrayList<String>();

 JSONArray galleryArray = detailObject.getJSONArray("interior_images");
for(int j=0; j<galleryArray.length(); j++){
  try {
        JSONObject jsonObject = galleryArray.getJSONObject(j);
        stringArray.add(jsonObject.toString());
    }
    catch (JSONException e) {
        e.printStackTrace();
    }
}
share|improve this answer

Try this way,hope this will help you to solve your problem.

try{
    String jsonRespone="{\"get\":[],\"post\":{\"_event\":\"get_buyers_guide_detail\",\"_deviceID\":\"490154203237518\",\"type\":\"cars\",\"model_id\":\"1007\"},\"cars\":[{\"ID\":6119,\"post_title\":\"COROLLA\",\"interior_images\":[\"http://.....e8664969d20654bf2a58f1b26de70d2.jpg\",\"http://........02/3bdfae250e3ce14514fe8f2f9dc3e58f.jpg\",\"http://........1d14a554a2ed78d1659463ec.jpg\"],}]}";
    JSONObject responeJson = new JSONObject(jsonRespone);
    ArrayList<HashMap<String,Object>> data = new ArrayList<HashMap<String, Object>>();
    JSONArray carsJsonArray = responeJson.getJSONArray("cars");
    for (int i=0;i<carsJsonArray.length();i++){
        HashMap<String,Object> row = new HashMap<String, Object>();
        row.put("ID",carsJsonArray.getJSONObject(i).getString("ID"));
        row.put("TITLE",carsJsonArray.getJSONObject(i).getString("post_title"));
        JSONArray galleryArray = carsJsonArray.getJSONObject(i).getJSONArray("interior_images");
        ArrayList<String> interiorImages = new ArrayList<String>();
        for(int j=0; j<galleryArray.length(); j++){
           interiorImages.add(galleryArray.getString(j));
        }
        row.put("INTERIORIMAGES",carsJsonArray.getJSONObject(i).getString("interiorImages"));
           data.add(row);
        }
        for (HashMap<String,Object> row :data){
            System.out.print("ID : "+row.get("ID"));
            System.out.print("TITLE : "+row.get("TITLE"));
            ArrayList<String> interiorImages =  (ArrayList<String>) row.get("INTERIORIMAGES");
            for (int j=0;j<interiorImages.size();j++){
                System.out.print("INTERIORIMAGES  +"+j+" : "+interiorImages.get(j));
            }
         }
  }catch (Throwable e){
     e.printStackTrace();
  }
share|improve this answer

I think you need to use List of Map

Example:

 List<Map> list = new ArrayList<Map>();
 Map map = new HashMap();
 map.put("userIcon", R.drawable.scott);
 map.put("username", "Shen");
 map.put("usertext", "This is a simple sample for SimpleAdapter");
 list.add(map);


 map = new HashMap();
 map.put("userIcon", R.drawable.ricardo);
 map.put("username", "Ricardo");
 map.put("usertext", "This is a simple sample for SimpleAdapter");
 list.add(map);

add this in loop

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.