2014-04-09 65 views
0

在我的Android 應用我有以下JSON字符串地圖轉換。我JSON數組跟隨,轉換JSON價值的關鍵削減到一個HashMap

[ 
    { 
    "WindDirection": { 
     "id": "3", 
     "wind_direction": "East", 
     "is_deleted": false 
    } 
    }, 
    { 
    "WindDirection": { 
     "id": "14", 
     "wind_direction": "East NorthEast", 
     "is_deleted": false 
    } 
    }, 
    { 
    "WindDirection": { 
     "id": "15", 
     "wind_direction": "East SouthEast", 
     "is_deleted": false 
    } 
    }, 
    { 
    "WindDirection": { 
     "id": "1", 
     "wind_direction": "North", 
     "is_deleted": false 
    } 
    }, 
    { 
    "WindDirection": { 
     "id": "10", 
     "wind_direction": "North NorthEast", 
     "is_deleted": false 
    } 
    }, 
    { 
    "WindDirection": { 
     "id": "11", 
     "wind_direction": "North NorthWest", 
     "is_deleted": false 
    } 
    }, 
    { 
    "WindDirection": { 
     "id": "7", 
     "wind_direction": "North West", 
     "is_deleted": false 
    } 
    }, 
    { 
    "WindDirection": { 
     "id": "6", 
     "wind_direction": "NorthEast", 
     "is_deleted": false 
    } 
    }, 
    { 
    "WindDirection": { 
     "id": "4", 
     "wind_direction": "South", 
     "is_deleted": false 
    } 
    }, 
    { 
    "WindDirection": { 
     "id": "12", 
     "wind_direction": "South SouthEast", 
     "is_deleted": false 
    } 
    }, 
    { 
    "WindDirection": { 
     "id": "13", 
     "wind_direction": "South SouthWest", 
     "is_deleted": false 
    } 
    }, 
    { 
    "WindDirection": { 
     "id": "8", 
     "wind_direction": "SouthEast", 
     "is_deleted": false 
    } 
    }, 
    { 
    "WindDirection": { 
     "id": "9", 
     "wind_direction": "SouthWest", 
     "is_deleted": false 
    } 
    }, 
    { 
    "WindDirection": { 
     "id": "2", 
     "wind_direction": "West", 
     "is_deleted": false 
    } 
    }, 
    { 
    "WindDirection": { 
     "id": "16", 
     "wind_direction": "West NorthWest", 
     "is_deleted": false 
    } 
    }, 
    { 
    "WindDirection": { 
     "id": "17", 
     "wind_direction": "West SouthWest", 
     "is_deleted": false 
    } 
    } 
] 

我希望把id和風向成哈希映射。這樣做的最簡單方法是什麼?

回答

0

試試這個..

HashMap<String,String> map_items = new HashMap<String,String>(); 
JSONArray jsonarray = new JSONArray(data); 

for (int i=0; i < jsonarray.length(); i++){ 
    JSONObject WindDirection_obj = jsonarray.getJSONObject(i).getJSONObject("WindDirection"); 
    map_items.put(WindDirection_obj.getString("id"),WindDirection_obj.getString("wind_direction")); 
} 
2

您可以使用GSON庫,你需要做模型的類。

代碼

public class SO1 { 
    public static void main(String[] args) { 
     Gson parser = new Gson(); 
     String json = "[ { \"WindDirection\": {\"id\": \"3\",\"wind_direction\": \"East\", \"is_deleted\": false } }, {\"WindDirection\": {  \"id\": \"14\",  \"wind_direction\": \"East NorthEast\",  \"is_deleted\": false } }]"; 
     JsonParser jParser = new JsonParser(); 
     JsonArray jArray = jParser.parse(json).getAsJsonArray(); 
    ArrayList<Model> modelList = new ArrayList<>(); 
     for (JsonElement element : jArray) { 
      JsonObject obj = (JsonObject) element; 
      element = obj.get("WindDirection"); 
      Model st = parser.fromJson(element, Model.class); 
      System.out.println(st); 
     modelList.add(st); 
     } 
    } 
} 

class Model { 
    String id; 
    String wind_direction; 
    boolean is_deleted; 
    @Override 
    public String toString() { 
     // TODO Auto-generated method stub 
     return id + "\t" + wind_direction + "\t" + is_deleted; 
    } 
} 

輸出

3 East false 
14 East NorthEast false