Putting JSON values into Hashmap Putting JSON values into Hashmap json json

Putting JSON values into Hashmap


Hashmap is a key/value storage, where keys are unique. You can convert your JSON to string and then store it as a value to the hashmap. For example something like below:

public static void main(String[] args) {        String json = "{ \"emp_id\": 1017,"                + "\"emp_name\": \"karthik Y\","                + "\"emp_designation\": \"Manager\","                + "\"department\": \"JavaJson\","                + "\"salary\": 30000,"                + "\"direct_reports\": ["                + "\"Nataraj G\","               + "\"Kalyan\","                + "\"Mahitha\"]}";         HashMap<String, String> jsonStore = new HashMap<String, String>();         jsonStore.put("myJson", json);         System.out.println(jsonStore.get("myJson"));    }

You need can also use the 'org.json' library to

  • Create JSON object manually
  • Convert existing JSONObject to String representation
  • Convert JSON string to JSONObject

You can also have the following solution:

JSONObject jsonObject = new JSONObject(); jsonObject.put("empt_id", 1017); jsonObject.put("emp_name", "karthik"); HashMap<String, JSONObject> jsonObjectStore = new HashMap<String, JSONObject>(); jsonObjectStore.put("myJsonObject", jsonObject); HashMap<JSONObject, String> jsonObjectStore2 = new HashMap<JSONObject, String>();jsonObjectStore2.put(jsonObject, "myJson"); 

Make sure that you download the org.json jar file and put it in your classpath to be able to use the JSONObject. You can download the jar from here.

In order to put each of those values into map as single key/value entry. You have mentioned it yourself, it should work without any problem. See below methods:

Method 1Everything in Java is Object, String inherits Object, String[] inherits object. You can have the following solution:

HashMap<String, Object> myObjectStore4 = new HashMap<String, Object>();String[] directReports4 = new String[]{"Natraj G", "Kalyan", "Mahitha"}; myObjectStore4.put("emp_id", new String("123")); myObjectStore4.put("emp_name", new String("Raf")); // others .... myObjectStore4.put("directReports", directReports4); 

Method 2 To store the fields as key/value and if you can afford converting the array to String (which represents all array elements comma separated then use this method).

HashMap<String, String> myObjectStoreTwo = new HashMap<String, String>();String[] directReports2 = new String[]{"Natraj G", "Kalyan", "Mahitha"}; myObjectStoreTwo.put("emp_id", "123"); myObjectStoreTwo.put("emp_name", "Raf"); myObjectStoreTwo.put("salary", "222");//Converts array to comma separated String myObjectStoreTwo.put("directReports",Arrays.toString(directReports2));

Method 3 In the expense of having Hash Map to store String key and Array value. You have to put other elements as array too.

HashMap<String, String[]> myObjectStore3 = new HashMap<String, String[]>();String[] directReports3 = new String[]{"Natraj G", "Kalyan", "Mahitha"}; myObjectStore3.put("emp_id", new String[]{123 + ""});myObjectStore3.put("salary", new String[]{32312 + ""}); myObjectStore3.put("directReports", directReports3);


Use a jackson ObjectMapper. Try if this works

String json = "{....}"HashMap<String,Object> mappedVals = new ObjectMapper().readValue(                    json ,                    new TypeReference<HashMap<String,Object>>() {                    });