猿问

通用 ResponseTo<T> 作为 Json

我正在努力反序列化JSON由通用结果结构包装的对象:


我JSON从服务器:


{

  "totalsize": 5,

  "data" : [

             {"name":1},

             {"name":2}

           ]

}

我的Java结果对象:


public class ResponseTo<T> {


    public Long totalsize = null;

    public final List<T> data = new ArrayList<>();

}

在这种情况下(可以是另一个 GET 请求中的任何其他内容)


public class Item {

    public int name;

}

在这种情况下T是一个Item.


如何使用以下方法将有效负载JSON反序列化到此对象结构中:

GSON和/或com.fasterxml.jackson?


我想要一个静态方法,例如:


public static <T> ResponseTo<T> stringToObject(String jsonString, Class<T> clazz ) {

    final Gson gson = new Gson();

    // do some typeadapter function magic

    return gson.fromJson( jsonString, ResponseTo.class );

}

它被调用


ResponseTo<Item> responseTo = stringToObject( <json-string>, Item.class );

我只接收com.google.gson.internal.LinkedTreeMap数据对象。


我该怎么做才能让它发挥作用?- 我做错了吗?


茅侃侃
浏览 172回答 3
3回答

侃侃无极

您需要使用类型标记来处理泛型情况,因为类型信息将T被 Java 编译器删除,Gson 不知道要恢复T到什么类型。Type&nbsp;typeToken&nbsp;=&nbsp;new&nbsp;TypeToken<ResponseTo<Item>>()&nbsp;{&nbsp;}.getType(); ResponseTo<Item>&nbsp;responseTo&nbsp;=&nbsp;stringToObject(&nbsp;<json-string>,&nbsp;typeToken&nbsp;);

慕婉清6462132

如果您在对象中有类型鉴别器(即每个示例中具有唯一值的公共字段),那么 Jackson@JsonTypeInfo(JsonTypeInfo.As.EXISTING_PROPERTY...)将允许您定义查找位置,并且初始化可以预先注册所有可能的类ObjectMapper.registerSubtypes(Item.class)。然后杰克逊会选择正确的那个。如果要实例化哪种类型的规则比“在字段 X 中查找”更复杂,那么您必须编写自己的规则@JsonTypeResolver来实现所述逻辑(非平凡)。

墨色风雨

格森您可以使用内部com.google.gson.internal.$Gson$Types类来实现这一点:import com.google.gson.Gson;import com.google.gson.internal.$Gson$Types;import java.lang.reflect.ParameterizedType;import java.util.ArrayList;import java.util.List;public class GsonApp {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; String json = "{\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; \"totalsize\": 5,\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; \"data\": [\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; {\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; \"name\": 1\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; },\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; {\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; \"name\": 2\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; }\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; ]\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "}\n";&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(stringToObject(json, Item.class));&nbsp; &nbsp; }&nbsp; &nbsp; static <T> ResponseTo<T> stringToObject(String jsonString, Class<T> clazz) {&nbsp; &nbsp; &nbsp; &nbsp; final Gson gson = new Gson();&nbsp; &nbsp; &nbsp; &nbsp; // do some typeadapter function magic&nbsp; &nbsp; &nbsp; &nbsp; ParameterizedType parameterizedType = $Gson$Types.newParameterizedTypeWithOwner(ResponseTo.class, ResponseTo.class, clazz);&nbsp; &nbsp; &nbsp; &nbsp; return gson.fromJson(jsonString, parameterizedType);&nbsp; &nbsp; }}class ResponseTo<T> {&nbsp; &nbsp; public Long totalsize = null;&nbsp; &nbsp; public final List<T> data = new ArrayList<>();&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return "ResponseTo{" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "totalsize=" + totalsize +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ", data=" + data +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '}';&nbsp; &nbsp; }}class Item {&nbsp; &nbsp; public int name;&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return "Item{" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name=" + name +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '}';&nbsp; &nbsp; }}上面的代码打印:ResponseTo{totalsize=5, data=[Item{name=1}, Item{name=2}]}杰克逊在Jackson你可以做类似的方法:import com.fasterxml.jackson.databind.JavaType;import com.fasterxml.jackson.databind.ObjectMapper;import java.io.IOException;public class JsonApp {&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; String json = "{\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; \"totalsize\": 5,\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; \"data\": [\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; {\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; \"name\": 1\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; },\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; {\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; \"name\": 2\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; }\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; ]\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "}\n";&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(stringToObject(json, Item.class));&nbsp; &nbsp; }&nbsp; &nbsp; static <T> ResponseTo<T> stringToObject(String jsonString, Class<T> clazz) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapper = new ObjectMapper();&nbsp; &nbsp; &nbsp; &nbsp; // do some typeadapter function magic&nbsp; &nbsp; &nbsp; &nbsp; JavaType responseType = mapper.getTypeFactory().constructParametricType(ResponseTo.class, clazz);&nbsp; &nbsp; &nbsp; &nbsp; return mapper.readValue(jsonString, responseType);&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答