将带有Gson的ArrayList转换为字符串 [英] Convert ArrayList with Gson to String
问题描述
我有一个ArrayList
,其中包含ArrayLists
,根"列表中的每个ArrayList
都包含一个ArrayList
的一个Integers
和一个Strings
的一个.我将Gson转换为String以使用SharedPreferences保存它.但是,当我对其进行转换时,Gson给了我2.131558489E9
而不是原始的int 2131558489
.我该如何解决此问题?最好的问候.
I have an ArrayList
which contains ArrayLists
, each ArrayList
in the Root list contains one ArrayList
of Integers
and one of Strings
. I am converting it with Gson to a String to save it with SharedPreferences. But when I am reconverting it, Gson gives me 2.131558489E9
instead of the original int 2131558489
. How can I fix this Problem? Best Regards.
这是我转换ArrayList的方法:levelPattern是ArrayList
Here is how I convert the ArrayList: levelPattern is the ArrayList
String levelPatternGson = new Gson().toJson(levelPattern);
这就是我将其转换回去的方式:
And this is how I convert it back:
levelPattern = new Gson().fromJson(levelPatternGson, ArrayList.class);
推荐答案
在json标准中,整数和双精度之间没有区别,只有数字类型.这就是为什么gson在默认情况下会在您不提供所需类型的情况下将数字转换为双精度数字的原因.
There is no difference in json standard between integers and doubles, there is only number type. That is why gson by default converts numbers to doubles if you don't give him what type you want.
简单的解决方法是使用TypeToken
并将数据结构更改为多个数组或自定义对象(例如@totoro
demo ).
Easy fix would be to use TypeToken
and change data structure to multiple arrays or custom object (like in @totoro
demo).
new Gson().fromJson(levelPatternGson, new TypeToken<List<Integer>>() {}.getType());
但是您也可以编写自定义列表反序列化器:
But you could also write custom List deserializer:
public static class ListDeserializerDoubleAsIntFix implements JsonDeserializer<List>{
@Override @SuppressWarnings("unchecked")
public List deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return (List) read(json);
}
public Object read(JsonElement in) {
if(in.isJsonArray()){
List<Object> list = new ArrayList<Object>();
JsonArray arr = in.getAsJsonArray();
for (JsonElement anArr : arr) {
list.add(read(anArr));
}
return list;
}else if(in.isJsonObject()){
Map<String, Object> map = new LinkedTreeMap<String, Object>();
JsonObject obj = in.getAsJsonObject();
Set<Map.Entry<String, JsonElement>> entitySet = obj.entrySet();
for(Map.Entry<String, JsonElement> entry: entitySet){
map.put(entry.getKey(), read(entry.getValue()));
}
return map;
}else if( in.isJsonPrimitive()){
JsonPrimitive prim = in.getAsJsonPrimitive();
if(prim.isBoolean()){
return prim.getAsBoolean();
}else if(prim.isString()){
return prim.getAsString();
}else if(prim.isNumber()){
Number num = prim.getAsNumber();
// here you can handle double int/long values
// and return any type you want
// this solution will transform 3.0 float to long values
if(Math.ceil(num.doubleValue()) == num.longValue())
return num.longValue();
else{
return num.doubleValue();
}
}
}
return null;
}
}
并像这样使用它:
GsonBuilder builder=new GsonBuilder();
List<List> levelPattern = Arrays.asList(Arrays.asList(2131558489L, 2L, 3L),
Arrays.asList("one", "two", "three"));
String levelPatternGson = new Gson().toJson(levelPattern);
List levelPattern2 = new GsonBuilder()
.registerTypeAdapter(List.class, new ListDeserializerDoubleAsIntFix())
.create()
.fromJson(levelPatternGson, List.class);
System.out.println(levelPattern2);
Json:[[2131558489,2,3],[一个",两个",三个"]]
Json: [[2131558489,2,3],["one","two","three"]]
输出:[[2131558489,2,3],[一,二,三]]
Output: [[2131558489, 2, 3], [one, two, three]]
这篇关于将带有Gson的ArrayList转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!