0

I have A JSON where I am getting "Floor", "flat_no" and "Flat_id". So I am getting them like this.

{
"results": [
    {
        "Flat_id": "1",
        "cat": "2",
        "Flat_no": "101",
        "Floor": "1",
        "Flat_type": "1 bhk",
    },
    {
        "Flat_id": "2",
        "cat": "2",
        "Flat_no": "102",
        "Floor": "1",
        "Flat_type": "2 bhk",
    },
    {
        "Flat_id": "3",
        "cat": "2",
        "Flat_no": "103",
        "Floor": "1",
        "Flat_type": "3 bhk",
    },
    {
        "Flat_id": "4",
        "cat": "2",
        "Flat_no": "104",
        "Floor": "1",
        "Flat_type": "1 rk",
    },
    {
        "Flat_id": "5",
        "cat": "2",
        "Flat_no": "201",
        "Floor": "2",
        "Flat_type": "1 bhk",
        "Floor plan": "http://vaserp.com/floor_plan/img1_1157img1_354454.png"
    },
    {
        "Flat_id": "6",
        "cat": "2",
        "Flat_no": "202",
        "Floor": "2",
        "Flat_type": "2 bhk",
        "Floor plan": "http://vaserp.com/floor_plan/img1_218706img1_1188632.png"
    },
    {
        "Flat_id": "7",
        "cat": "2",
        "Flat_no": "203",
        "Floor": "2",
        "Flat_type": "3 bhk",
    },
    {
        "Flat_id": "8",
        "cat": "2",
        "Flat_no": "204",
        "Floor": "2",
        "Flat_type": "1 rk",
    }
]

}

I know JSON parsing. But I need A "Floor", and after that all flat and their id of that particular "Floor". I've attached an image, which show how I need to show the data ? Where "22" is floor and "201 to 205" are flat no. So please help me how can I sort this problem ?

I tried like this

try {
    JSONObject jsonObj = new JSONObject(jsonStr);
    allFloorDetailArray = jsonObj.getJSONArray(JSONUrl.TAG_RESULT);
    for (int i = 0; i < allFloorDetailArray.length(); i++) {
        JSONObject c = allFloorDetailArray.getJSONObject(i);
        FloorDetailItem floorDetailItem = new FloorDetailItem();
        floorDetailItem.setCat(c.getString(JSONUrl.TAG_CAT));
        floorDetailItem.setFlatNo(c.getString(JSONUrl.TAG_FLAT_NO));
        floorDetailItem.setFloor(c.getString(JSONUrl.TAG_FLOOR));
        floorNo = c.getString(JSONUrl.TAG_FLOOR);
        floorDetailItem.setFlatType(c.getString(JSONUrl.TAG_FLATE_TYPE));
        floorDetailItem.setFloorPlanImage(c.getString(JSONUrl.TAG_FLOOR_PLAN));

        mListDetailFloor.add(floorDetailItem);

    }
} catch (JSONException e) {
    e.printStackTrace();
}

enter image description here

9
  • I didn't get your problem. please explain more! Commented Nov 17, 2014 at 10:28
  • I mean i need floor no at the place of 22(see image), and as you can see I have "Floor" in every object. So it must come only 1 time and its all flat must com like 201 to 205(see image). SO I mean I need Floor 1 time and its all flat must come along with. Commented Nov 17, 2014 at 10:31
  • @AmitJayaswal you need to parse whole JSON and you need to manipulate data to show. it will not be a good way to leave some data while parsing. Commented Nov 17, 2014 at 10:32
  • I mean I am getting Floor "1" 4 times, but it must show only 1 time and the flat 101, 102, 103, 104, must show like the image Commented Nov 17, 2014 at 10:33
  • 1
    you can parse your whole json and group your objects by floor no. Commented Nov 17, 2014 at 10:33

2 Answers 2

1

Try to use HashMap with floor as key and flat list as value :

Note : Your Json response is invalid in some flat json object like when "Floor plan" not came in flat json after "Flat_type" (,) comma is invalid

String jsonString = "{\"results\":[{\"Flat_id\":\"1\",\"cat\":\"2\",\"Flat_no\":\"101\",\"Floor\":\"1\",\"Flat_type\":\"1 bhk\"},{\"Flat_id\":\"2\",\"cat\":\"2\",\"Flat_no\":\"102\",\"Floor\":\"1\",\"Flat_type\":\"2 bhk\"},{\"Flat_id\":\"3\",\"cat\":\"2\",\"Flat_no\":\"103\",\"Floor\":\"1\",\"Flat_type\":\"3 bhk\"},{\"Flat_id\":\"4\",\"cat\":\"2\",\"Flat_no\":\"104\",\"Floor\":\"1\",\"Flat_type\":\"1 rk\"},{\"Flat_id\":\"5\",\"cat\":\"2\",\"Flat_no\":\"201\",\"Floor\":\"2\",\"Flat_type\":\"1 bhk\",\"Floor plan\":\"http://vaserp.com/floor_plan/img1_1157img1_354454.png\"},{\"Flat_id\":\"6\",\"cat\":\"2\",\"Flat_no\":\"202\",\"Floor\":\"2\",\"Flat_type\":\"2 bhk\",\"Floor plan\":\"http://vaserp.com/floor_plan/img1_218706img1_1188632.png\"},{\"Flat_id\":\"7\",\"cat\":\"2\",\"Flat_no\":\"203\",\"Floor\":\"2\",\"Flat_type\":\"3 bhk\"},{\"Flat_id\":\"8\",\"cat\":\"2\",\"Flat_no\":\"204\",\"Floor\":\"2\",\"Flat_type\":\"1 rk\"}]}";
HashMap<String,Object> floorMap = new HashMap<String, Object>();
try{
     JSONObject responseJson =  new JSONObject(jsonString);
     JSONArray  resultJsonArray = responseJson.getJSONArray("results");

     for (int i=0;i<resultJsonArray.length();i++){
          if(floorMap.containsKey(resultJsonArray.getJSONObject(i).getString("Floor"))){
              ArrayList<HashMap<String,String>> flatList = (ArrayList<HashMap<String,String>>) floorMap.get(resultJsonArray.getJSONObject(i).getString("Floor"));
              HashMap<String,String> flatMap = new HashMap<String, String>();
              flatMap.put("Flat_id",resultJsonArray.getJSONObject(i).getString("Flat_id"));
              flatMap.put("Flat_no",resultJsonArray.getJSONObject(i).getString("Flat_no"));
              flatList.add(flatMap);
              floorMap.put(resultJsonArray.getJSONObject(i).getString("Floor"),flatList);
          }else{
              ArrayList<HashMap<String,String>> flatList = new ArrayList<HashMap<String, String>>();
              HashMap<String,String> flatMap = new HashMap<String, String>();
              flatMap.put("Flat_id",resultJsonArray.getJSONObject(i).getString("Flat_id"));
              flatMap.put("Flat_no",resultJsonArray.getJSONObject(i).getString("Flat_no"));
              flatList.add(flatMap);
              floorMap.put(resultJsonArray.getJSONObject(i).getString("Floor"),flatList);
          }
    }

}catch (Throwable e){
    e.printStackTrace();
}

Iterator it = floorMap.entrySet().iterator();
while (it.hasNext()) {
   Map.Entry pairs = (Map.Entry)it.next();
   System.out.print("Floor :: "+pairs.getKey());
   ArrayList<HashMap<String,String>> flatList = (ArrayList<HashMap<String,String>>) pairs.getValue();
   for(HashMap<String,String> flat : flatList){
       System.out.print("Flat_id :: "+flat.get("Flat_id"));
       System.out.print("Flat_no :: "+flat.get("Flat_no"));
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

If your 'Floor' is unique, you can use HashMap.

HashMap<int,Yourflatobjectlist> data = new HashMap<int,Yourflatobjectlist>();

data.put(floornumber as a key, List of Flat present at that Floor) ; 

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.