关于Gson对于Array和List的 serialize 输出. 都是中括号括起来的.
Gson gson=new Gson();
String[] arrays=new String[]{"a","b","c"};
List<String> list=new ArrayList<String>();
list.add("listA");
list.add("listB");
list.add("listC");
System.out.println(gson.toJson(arrays));//["a","b","c"]
System.out.println(gson.toJson(list));//["listA","listB","listC"]
所以需要在deserialize中注意泛型的匹配.
String jsonArrayStr="[\"arrayA\",\"arrayB\",\"arrayC\"]";
Type arrayType=new TypeToken<String[]>(){}.getType();
String[] rstArray=gson.fromJson(jsonArrayStr,arrayType);
for (String s : rstArray) {
System.out.println(s);//arrayA //arrayB //arrayC
}
String jsonListStr="[\"listA\",\"listB\",\"listC\"]";
Type listType=new TypeToken<List<String>>(){}.getType();
List<String> rstList=gson.fromJson(jsonListStr,listType);
for (String s : rstList) {
System.out.println(s);//listA //listB //listC
}