如何从JSON对象获取特定值?

时间:2014-11-17 10:25:30

标签: java android json getjson

我有一个JSON,我得到“Floor”,“flat_no”和“Flat_id”。所以我这样得到它们。

{
"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",
    }
]

}

我知道JSON解析。但我需要一个“地板”,然后是所有的平面和他们的特定“地板”的ID。我附上了一张图片,上面显示了我需要如何显示数据?其中“22”是地板,“201至205”是平地。 那么请帮助我如何解决这个问题呢?

我试过这个

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

2 个答案:

答案 0 :(得分:1)

尝试将HashMap与floor作为键,将flat list用作值:

注意:您的Json响应在某些平面json对象中无效,例如&#34; Floor plan&#34;在&#34; Flat_type&#34;之后没有进入扁平的json。 (,)逗号无效

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

答案 1 :(得分:0)

如果您的“楼层”是唯一的,则可以使用HashMap

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

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