public class DateTest {
public static void main(String[] args) throws Exception {
// object
String cacheData = "{\"personId\":1111}";
Gson gson = new GsonBuilder().disableHtmlEscaping()
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.create();
Person p = gson.fromJson(cacheData, Person.class);
System.out.println(p.getPersonId());
//list 不能使用List.class 否则会报LinkedTreeMap cannot be cast to xxx
cacheData = "[{\"personId\":1111}]";
gson = new GsonBuilder().disableHtmlEscaping()
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.create();
List<Person> ps = null;
//ps = gson.fromJson(cacheData, List.class);
Type type = new TypeToken<List<Person>>(){}.getType();
ps = gson.fromJson(cacheData, type);
for (Person person : ps) {
System.out.println(person.getPersonId());
}
}
}
@Data
class Person {
private Integer personId;
}