Json字符串包含整数值,而反序列化为HashMap,则整数转换为双精度值 [英] Json String contain integer value , while deserialize to HashMap, Integer convert into double values
本文介绍了Json字符串包含整数值,而反序列化为HashMap,则整数转换为双精度值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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);
/ *以下输出:
*
JsonString :: {
num4:\\\<\\\>,
num5:〜 !@#$%^ \\\& *()_ + \\\=-`,.\\\<\\\>?/ :; [] {} |,
num2:20,
num3:20.0,
num1:10
}
* /
h = g.fromJson(jsonStr,HashMap.class);
System.out.println(convert from json String:>+ h);
/ *以下输出:
从json字符串转换:> {num4 =<> ;, num5 =〜!@#$%^& *()_ + = - `,。< ;>?/ :; [] {} |,num2 = 20.0,num3 = 20.0,num1 = 10.0}
* /
int num1 =(Integer)h.get( NUM1\" );
System.out.println(num1);
线程main中的异常java.lang.ClassCastException:java.lang.Double不能转换为java.lang.Integer
在com.ps.multiupload.servlet.FileUploadUtil.main(FileUploadUtil.java :52)
解决方案
输入你想要的。否则,它总是使用它最喜欢的类型:Map,List,String,Double和Boolean。为了序列化你的混合类型哈希映射,创建一个Java类知道需要哪种类型:
class NumNumNum {
int num1;
int num2;
double num3;
字符串num4;
字符串num5;
}
将JSON反序列化为这样的类会给Gson提供它需要的提示。只需一个 Map< Object,Object>
它就是最简单的事情。
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)
解决方案
Gson works best if you tell it the type you want. Otherwise it'll always just use its favorite types: Map, List, String, Double, and Boolean.
To serialize your mixed types hash map, create a Java class that knows which types it wants:
class NumNumNum {
int num1;
int num2;
double num3;
String num4;
String num5;
}
Deserializing JSON into a class like that will give Gson the hints it needs. With just a Map<Object, Object>
it just does the simplest thing.
这篇关于Json字符串包含整数值,而反序列化为HashMap,则整数转换为双精度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文