1

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

8
  • 4
    Your JSON is not valid. Commented Jul 4, 2014 at 4:38
  • @Prince im only showing here my important part Commented Jul 4, 2014 at 4:39
  • 2
    show your complete JSON Commented Jul 4, 2014 at 4:42
  • 1
    @user3751280 keys are very important in JSON. You are missing them. Commented Jul 4, 2014 at 4:48
  • 1
    possible duplicate of JSON Array of strings (no objects), extracting data Commented Jul 4, 2014 at 4:53

5 Answers 5

1

Try this,

galleryArray.getString(j);

this should get the string at position j.

Sign up to request clarification or add additional context in comments.

Comments

1

galleryArray.getString(index);

This function will return String object of corresponding index

Comments

0

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

Comments

0

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

Comments

0

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

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.