String->List
字段AA 值[“1”,“2”] 类型 test
List<String> projectIdList = gson.fromJson(AA,new TypeToken<List<String>>(){}.getType());
转完之后的结果为[1,2]
对象转Json
生成Json字符串
Gson gson = new Gson();
List < Person > persons = new ArrayList < Person > ();
for ( int i = 0 ; i < 3 ; i ++ ) {
Person p = new Person();
p.setName( " name " + i);
p.setAge(i * 5 );
persons.add(p);
}
String str = gson.toJson(persons);
上面的代码重点是Gson对象,它提供了toJason()方法将对象转换成Json字符串,上面代码的str对象值为:
[{"name":"name0","age":0},{"name":"name1","age":5},{"name":"name2","age":10},