2

I have JSON value like below,

   { "emp_id": 1017,
   "emp_name": "karthik Y", 
  "emp_designation": "Manager", 
   "department": "JavaJson", 
   "salary": 30000, 
   "direct_reports":
  [ 
  "Nataraj G",
   "Kalyan", 
  "Mahitha" 
  ] 
} 

 HashMap < String, String[] >input1 = new HashMap < String, String[] >();
 input1.put("empid","1017");
 input1.put("emp_name","karthik");
 input1.put("emp_designation","manager");
 input1.put("salary","30000");

now I want to add next array that is direct_report to put as next key and value(entire array shoud be come one key and value). Someone please help out.

  • 1
    what has to be in key and value ? – Thanga Nov 25 '15 at 10:58
  • Map dbInputMap = new HashMap(); dbInputMap.put("Parm1", new String[]{emp_id+ ""}); dbInputMap.put("Parm2", new String[]{emp_name + ""}); dbInputMap.put("Parm3", new String[]{emp_designation+ ""}); dbInputMap.put("Parm4", new String[]{department+ ""}); dbInputMap.put("Parm5", new String[]{salary+ ""}) I also want to put the direct report array to this map and I have pass this map to somewhere. –  Nov 25 '15 at 11:05
  • Possible duplicate of [How to convert a JSON string to a Map with Jackson JSON](http://stackoverflow.com/questions/2525042/how-to-convert-a-json-string-to-a-mapstring-string-with-jackson-json) – rupesh_padhye Nov 25 '15 at 11:20

2 Answers2

2

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 1 Everything 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);
Raf
  • 6,620
  • 1
  • 35
  • 54
  • Direct_report key contains a set of array values. how do I set the set of array value into single key and value. –  Nov 25 '15 at 11:31
  • To understand it properly your direct report is something like this > String[] directReports = {"Natraj G", "Kalyan", "Mahitha"}; right? if yes, then you could easily add it to a Map which you mentioned in your question. – Raf Nov 25 '15 at 11:35
  • I have modified my question, look once again. –  Nov 25 '15 at 12:08
  • There are a few methods to do it, I will update the answer to list all the method. – Raf Nov 25 '15 at 12:28
1

Use a jackson ObjectMapper. Try if this works

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