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;
                    }
                };
            }
        }
    }
    

      

  • 相关阅读:
    win10下安装Anaconda3+keras+tensorflow
    Ubuntu 下安装安装 Anaconda3 与Keras
    ESP32开发之Windows开发环境
    LinuxE2系统刷机后OSCAM安装与读卡器设置
    安德鲁1.2Ku全下125C波(CCTV4K除外)
    安德鲁1.2Ku使用感受
    ubuntu10.04换官方源
    密室逃脱游戏解决方案-森林迷宫-炸弹人等
    Qt linux获取cpu使用率、内存、网络收发速度、磁盘读写速度、磁盘剩余空间等
    ESP32 做Web服务器 http Server步骤
  • 原文地址:https://www.cnblogs.com/areful/p/13869060.html
Copyright © 2011-2022 走看看