使用FastJSON將字符串類型的JSON對象轉爲泛型對象類型,需要特殊處理下才能正常轉換。
json使用的是fastjson,json轉換對象的時候,如果對象中存在泛型對象,則需要特殊處理下才能正常轉換。
使用的是fastjson中的TypeReference來進行轉換:
A<B<C>> resultObj =JSON.parseObject("轉換json",new TypeReference<A<B<C>>>(){});
其中A爲接收類型,B爲A的泛型類,C爲B的泛型類
例如:將
{"code":"0","msg":null,"data":["a36a655206394154a14918d6aa6d73f41"]}
轉爲
public class ActionResult<T> implements Serializable {
private static final long serialVersionUID = 5744675651322306036L;
private String code;
private String msg;
private T data;
}
泛型接受爲Set類型時,可以使用
String resultString = "{\"code\":\"0\",\"msg\":null,\"data\":[\"a36a655206394154a14918d6aa6d73f41\"]}";
ActionResult<Set<String>>result = JSON.parseObject(resultString,new TypeReference<ActionResult<Set<String>>>(){});
當然也可以轉爲List類型