Java 将json值密钥pare转换为hashmap
在我的android应用程序中,我必须将以下json字符串转换为映射。我的json数组如下所示:Java 将json值密钥pare转换为hashmap,java,android,json,Java,Android,Json,在我的android应用程序中,我必须将以下json字符串转换为映射。我的json数组如下所示: [ { "WindDirection": { "id": "3", "wind_direction": "East", "is_deleted": false } }, { "WindDirection": { "id": "14", "wind_direction": "East NorthEast",
[
{
"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和风向放到一个散列图中。最简单的方法是什么。试试这个
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"));
}
HashMap\u items=newhashmap();
JSONArray JSONArray=新的JSONArray(数据);
for(int i=0;i
您可以使用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