zoukankan      html  css  js  c++  java
  • alibaba fastjson TypeReference 通过字符串反射返回对象

    TypeReference
    EditNew Page
    温绍 edited this page Nov 3, 2017 · 8 revisions
    1. 基础使用
    在fastjson中提供了一个用于处理泛型反序列化的类TypeReference。

    import com.alibaba.fastjson.TypeReference;

    List<VO> list = JSON.parseObject("...", new TypeReference<List<VO>>() {});
    如下写法有更好的性能

    import com.alibaba.fastjson.TypeReference;

    final static Type type = new TypeReference<List<VO>>() {}.getType();

    List<VO> list = JSON.parseObject("...", type);
    在这里例子中,通过TypeReference能够解决List中T的类型问题。

    2. 带参数使用
    在1.2.9 & 1.1.49.android版本中,TypeReference支持泛型参数,方便一些框架实现通用的反序列化类。用法如下:

    2.1. 单参数例子

    public class Response<T> {
    public T data;
    }
    public static <T> Response<T> parseToMap(String json, Class<T> type) {
    return JSON.parseObject(json,
    new TypeReference<Response<T>>(type) {});
    }
    2.2. 双参数例子

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

    // 可以这样使用
    String json = "{1:{name:"ddd"},2:{name:"zzz"}}";
    Map<Integer, Model> map = parseToMap(json, Integer.class, Model.class);
    assertEquals("ddd", map.get(1).name);
    assertEquals("zzz", map.get(2).name);

    来源:https://github.com/alibaba/fastjson/wiki/TypeReference

    https://www.programcreek.com/java-api-examples/org.codehaus.jackson.type.TypeReference

  • 相关阅读:
    WPF基础之内容控件
    WPF基础之路由事件
    WPF基础分享之布局
    JMeter操作手册
    Jmeter安装和配置
    UI自动化--Web Driver小结
    对于自动化测试框架的总结
    UI自动化--selenium webdriver
    postman断言
    接口测试工具---postman的基本使用
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/9999232.html
Copyright © 2011-2022 走看看