public static void main(String[] args) {
Gson g = new GsonBuilder()
.setPrettyPrinting()
.enableComplexMapKeySerialization()
.serializeSpecialFloatingPointValues()
.setLongSerializationPolicy(LongSerializationPolicy.DEFAULT)
.setPrettyPrinting()
//.registerTypeAdapter(HashMap.class, new HashMapDeserializer())
.create();
HashMap<Object, Object> h = new HashMap<Object, Object>();
h.put("num1", 10);
h.put("num2", 20);
h.put("num3", 20.0);
h.put("num4", "<>");
h.put("num5", "~!@#$%^&*()_+=-`,.<>?/:;[]{}|");
String jsonStr = g.toJson(h);
System.out.println("JsonString::"+jsonStr);
/*Output below ::
*
JsonString::{
"num4": "\u003c\u003e",
"num5": "~!@#$%^\u0026*()_+\u003d-`,.\u003c\u003e?/:;[]{}|",
"num2": 20,
"num3": 20.0,
"num1": 10
}
*/
h = g.fromJson(jsonStr, HashMap.class);
System.out.println("convert from json String :>"+h);
/*Output below:
convert from json String :>{num4=<>, num5=~!@#$%^&*()_+=-`,.<>?/:;[]{}|, num2=20.0, num3=20.0, num1=10.0}
*/
int num1= (Integer) h.get("num1");
System.out.println(num1);
}
Exception in thread "main" java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer
at com.ps.multiupload.servlet.FileUploadUtil.main(FileUploadUtil.java:52)