Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

TypeReference

wenshao edited this page Apr 16, 2016 · 8 revisions

在fastjson中提供了一个用于处理泛型反序列化的类TypeReference。

import com.alibaba.fastjson.TypeReference;

List<VO> list = JSON.parseObject("...", new TypeReference<List<VO>>() {});

在这里例子中,通过TypeReference能够解决List中T的类型问题。

在1.2.9 & 1.1.49.android版本中,TypeReference支持泛型参数,用法如下:

public static <K, V> Map<K, V> getJsonToMap(String json, Class<K> keyType, Class<V> valueType) {
    return JSON.parseObject(json, new TypeReference<Map<K, V>>(keyType, valueType) {});
}

public void test_for_issue_2() throws Exception {
    String json = "{1:{name:\"ddd\"},2:{name:\"zzz\"}}";
    Map<Integer, Model> map = getJsonToMap(json, Integer.class, Model.class);
    Assert.assertEquals("ddd", map.get(1).name);
    Assert.assertEquals("zzz", map.get(2).name);
}
Clone this wiki locally