Samantha Withanage
Samantha Withanage

Reputation: 3851

converting json value key pare in to a hashmap

In my android application I have to convert following json string into a map. my json array is following,

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

i want to put the id and wind direction into a hash map. what is the easiest way of doing this.

Upvotes: 0

Views: 116

Answers (2)

Gaurav Gupta
Gaurav Gupta

Reputation: 4701

You can use Gson library and you need to make a Model class.

code

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

Output

3   East    false
14  East NorthEast  false

Upvotes: 2

Hariharan
Hariharan

Reputation: 24853

Try this..

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

Upvotes: 0

Related Questions