zoukankan      html  css  js  c++  java
  • Java中使用Gson解析泛型类型数据

    一直想找个通用的解析JSON数据模板而不得,自己琢磨了两天,整出来一套代码,现将核心代码整理如下,后人见此可少踩坑矣:

    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    
    import java.lang.reflect.ParameterizedType;
    import java.lang.reflect.Type;
    
    /**
     * Created by areful on 2020/10/24.
     */
    public class TestParseGenerateType {
    
        public static void main(String[] args) {
            testParseResBody1();
            testParseResBody2();
        }
    
        private static void testParseResBody1() {
            String json = "{"body":{"name":"areful"}}";
            ResponseType<ResBody1> r = new Parser<ResBody1>() {
            }.parse(json);
    
            assert r != null;
            ResBody1 resBody1 = r.getBody();
            System.out.println(resBody1.name);
        }
    
        private static void testParseResBody2() {
            String json = "{"body":{"code":1997}}";
            ResponseType<ResBody2> r = new Parser<ResBody2>() {
            }.parse(json);
    
            assert r != null;
            ResBody2 resBody2 = r.getBody();
            System.out.println(resBody2.code);
        }
    
        public static class ResBody1 {
            public String name;
        }
    
        public static class ResBody2 {
            public int code;
        }
    
        public static class ResponseType<T> {
            private T body;
    
            public T getBody() {
                return body;
            }
        }
    
        private static class Parser<T> {
            private static final Gson gson = new GsonBuilder().create();
            private final Class<?> clazz = ResponseType.class;
    
            public ResponseType<T> parse(String json) {
                try {
                    ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
                    Type objectType = buildType(clazz, type.getActualTypeArguments());
                    return gson.fromJson(json, objectType);
                } catch (Exception ignored) {
                }
                return null;
            }
    
            private static ParameterizedType buildType(final Class<?> raw, final Type... args) {
                return new ParameterizedType() {
                    public Type getRawType() {
                        return raw;
                    }
    
                    public Type[] getActualTypeArguments() {
                        return args;
                    }
    
                    public Type getOwnerType() {
                        return null;
                    }
                };
            }
        }
    }
    

      

  • 相关阅读:
    selector在手机上或浏览器显示各种姿势(虚拟下拉菜单)
    关于JavaScript禁止点击事件
    设为主页以及其它功能实现
    判断浏览器是否支持flash
    渐进式增强
    判断用户Input输入的事件来进行登陆
    移动端底部input被弹出的键盘遮挡
    关于中间文字实现
    关于小程序navigator没有高的情况
    M.2接口NVMe协议的固态硬盘读写速度是SATA接口的两倍
  • 原文地址:https://www.cnblogs.com/areful/p/13869060.html
Copyright © 2011-2022 走看看