GSON 同字段不同类型要怎么解析

时间:2022-01-03 10:26:55

GSON 同字段不同类型要怎么解析?

求大神不吝赐教!
求大神不吝赐教!
求大神不吝赐教!

主要是"auth_info"这一项存在同一字段不同类型的情况,其它我已经解析出来,但是这一个总是返回一个“null"


以下是平台回传JSON格式的文件内容

{
    "errno": 0,
    "data": {
        "per_page": 30,
        "devices": [
            {
                "private": false,
                "route_to": "http://www.baidu.com",
                "create_time": "2016-07-29 10:26:55",
                "online": false,
                "id": "3223476",
                "auth_info": {
                    "SYS": "Wspin9AMxmpx5SS1m9HMNVD7JPk="
                },
                "title": "Ap测试",
                "desc": "Ap测试"
            },
            {
                "private": false,
                "route_to": "http://www.baidu.com",
                "create_time": "2016-07-29 10:22:56",
                "online": false,
                "id": "3223475",
                "auth_info": {
                    "SYS": "MHzBAdLShkzT9d6UGn=nE6S8LK8="
                },
                "title": "Ap测试",
                "desc": "Ap测试"
            },
            {
                "private": false,
                "route_to": "http://www.baidu.com",
                "create_time": "2016-07-29 10:19:54",
                "online": false,
                "id": "3223474",
                "auth_info": {
                    "SYS": "wAuEux6UfLze6ex405CBhWKxh3I="
                },
                "title": "OneNetApi测试",
                "desc": "OneNetApi测试"
            },
            {
                "private": false,
                "route_to": "http://www.baidu.com",
                "create_time": "2016-07-29 10:19:03",
                "online": false,
                "id": "3223472",
                "auth_info": {
                    "SYS": "by1jiggWuLda0QSHA2=TDBnhy=I="
                },
                "title": "Ap测试",
                "desc": "Ap测试"
            },
            {
                "private": false,
                "route_to": "http://www.baidu.com",
                "create_time": "2016-07-27 21:45:07",
                "online": false,
                "id": "3222010",
                "auth_info": {
                    "SYS": "WmvSnvwv=EjFvgTD2Jm767w63ck="
                },
                "title": "Ap测试",
                "desc": "Ap测试"
            },
            {
                "private": false,
                "route_to": "http://www.baidu.com",
                "create_time": "2016-07-26 15:40:04",
                "online": false,
                "id": "3220758",
                "auth_info": {
                    "SYS": "=sTLItzd5pHWSGEgGlNxjIyRTcs="
                },
                "title": "Ap测试",
                "desc": "Ap测试"
            },
            {
                "private": false,
                "protocol": "HTTP",
                "route_to": "http://www.baidu.com",
                "create_time": "2016-07-22 11:43:29",
                "online": false,
                "location": {
                    "lon": 114.02515252835999,
                    "lat": 22.673693004784
                },
                "auth_info": "2016072201",
                "id": "3213244",
                "title": "Ap测试",
                "desc": "Ap测试",
                "tags": []
            },
            {
                "private": false,
                "protocol": "HTTP",
                "route_to": "http://www.baidu.com",
                "create_time": "2016-07-22 10:49:52",
                "online": false,
                "location": {
                    "lon": 0,
                    "lat": 0
                },
                "auth_info": "3213230",
                "id": "3213230",
                "title": "Ap测试1",
                "desc": "Ap测试1",
                "tags": []
            },
            {
                "private": false,
                "route_to": "http://www.baidu.com",
                "create_time": "2016-07-22 10:49:08",
                "online": false,
                "id": "3213229",
                "auth_info": {
                    "SYS": "Po1Cj1OK1v4ah5FZeckgNZ9q==8="
                },
                "title": "Ap测试",
                "desc": "Ap测试"
            },
            {
                "private": false,
                "route_to": "http://www.baidu.com",
                "create_time": "2016-07-22 10:48:24",
                "online": false,
                "id": "3213227",
                "auth_info": {
                    "SYS": "buIuMxoKJZjUP3ghqUrFh2Q7jks="
                },
                "title": "Ap测试",
                "desc": "Ap测试"
            },
            {
                "private": true,
                "create_time": "2016-05-07 22:26:13",
                "online": false,
                "id": "1100928",
                "auth_info": {
                    "SYS": "OPXSMf3OoB93=Fn5S0x9qtRVzqQ="
                },
                "title": "体验用设备",
                "desc": "该设备为体验用设备,由程序自动创建",
                "tags": []
            },
            {
                "private": true,
                "create_time": "2016-05-05 13:54:42",
                "online": false,
                "id": "1100536",
                "auth_info": {
                    "SYS": "cRDUW69=qBIMnlq=HtO3wtw65TI="
                },
                "title": "测试",
                "desc": "测试",
                "tags": []
            },
            {
                "private": false,
                "protocol": "HTTP",
                "create_time": "2016-05-05 09:19:00",
                "online": false,
                "location": {
                    "lon": 113.83576276301,
                    "lat": 22.603091462851906
                },
                "auth_info": {
                    "SYS": "dzMHq8EfaNDMSPc=OTdD3qGtdL0="
                },
                "id": "1100467",
                "title": "GPS_Test",
                "desc": "测试",
                "tags": [
                    "测试"
                ]
            }
        ],
        "total_count": 18,
        "page": 1
    },
    "error": "succ"
}

15 个解决方案

#1


JSONObject  auth_info =  JSONObject.getJSONObject("auth_info "); 
String sys  = auth_info .getString("SYS");

因为auth_info   也是一个 json 结构 获取 auth_info  信息的时候要用JSONObject 来接收  然后再获取 sys 的值
标红的 要改为你的变量 名

#2


@yang1912 org.json我已经解析出来了,目前我要的是基于Google的GSON解析。

#3


 "auth_info": {
                    "SYS": "cRDUW69=qBIMnlq=HtO3wtw65TI="
          },
 "auth_info": "3213230", 
解析不出吧
用gson 用的比较少
类似这种情况 就得自己写 解析规则 了 deserialize
用自己写的规则 去解析 反序列化  获得数据
实现 implements JsonDeserializer< >  标红是你的实体类

再具体 看下官方文档吧 挺全的
不好意思!

#4


Gson要的是格式统一吧。将格式不同 的那个改成统一的吧,需要用的时候做下判断呗

#5


@yang1912  用JACKSON可以简单地解析出来,目前已经做出来,而且可以判断,但是用GSON就在这一层出错啦。我也知道要用deserialize 但是目前没有可参考的Demo不知道兄弟有没有!有提供一下?

#6


@moonFY         Gson格式要怎样统一呢?还有定义的时候要怎么写呢?我都是直接使用GSON插件来生成的,调用过程中,总返回一个null, 在其它项也有出现返回null的情况,如private String routeTo;但是我后来在其上面添加@SerializedName("route_to") 就可以正常使用!

#7


这个是我那个Bean的初始代码:
帮忙看一下哪里出错啦


public class Bean{

    //@SerializedName("errno")
    private int errno;
  

    //@SerializedName("data")
    private DataBean data;
    //@SerializedName("error")
    private String error;

    public int getErrno() {
        return errno;
    }

    public void setErrno(int errno) {
        this.errno = errno;
    }

    public DataBean getData() {
        return data;
    }

    public void setData(DataBean data) {
        this.data = data;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public static class DataBean {
        @SerializedName("per_page")
        private int perPage;
        @SerializedName("total_count")
        private int totalCount;
        @SerializedName("page")
        private int page;
      

        //@SerializedName("devices")
        private List<DevicesBean> devices;

        public int getPerPage() {

            return perPage;
        }

        public void setPerPage(int perPage) {

            this.perPage = perPage;
        }

        public int getTotalCount() {

            return totalCount;
        }

        public void setTotalCount(int totalCount) {

            this.totalCount = totalCount;
        }

        public int getPage() {

            return page;
        }

        public void setPage(int page) {

            this.page = page;
        }

        public List<DevicesBean> getDevices() {

            return devices;
        }

        public void setDevices(List<DevicesBean> devices) {

            this.devices = devices;
        }

        public static class DevicesBean {
            //@SerializedName("private")
            private boolean privateX;
            @SerializedName("protocol")
            private String protocol;
            @SerializedName("route_to")
            private String routeTo;
            @SerializedName("create_time")
            private String createTime;
            //@SerializedName("online")
            private boolean online;
           

            @SerializedName("location")
            private LocationBean location;
            private AuthInfoBean authinfobean;






            //@SerializedName("auth_info")

            //@SerializedName("id")
            private String id;
            //@SerializedName("title")
            private String title;
            //@SerializedName("desc")
            private String desc;
            //@SerializedName("tags")
            private List<String> tags;


            public boolean isPrivateX() {

                return privateX;
            }

            public void setPrivateX(boolean privateX) {

                this.privateX = privateX;
            }

            public String getProtocol() {

                return protocol;
            }

            public void setProtocol(String protocol) {

                this.protocol = protocol;
            }

            public String getRouteTo() {

                return routeTo;
            }

            public void setRouteTo(String routeTo) {

                this.routeTo = routeTo;
            }

            public String getCreateTime() {

                return createTime;
            }

            public void setCreateTime(String createTime) {

                this.createTime = createTime;
            }

            public boolean isOnline() {

                return online;
            }

            public void setOnline(boolean online) {

                this.online = online;
            }

            public LocationBean getLocation() {

                return location;
            }

            public void setLocation(LocationBean location) {

                this.location = location;
            }





            public String getId() {
                return id;
            }

            public void setId(String id) {
                this.id = id;
            }

            public String getTitle() {
                return title;
            }

            public void setTitle(String title) {
                this.title = title;
            }

            public String getDesc() {
                return desc;
            }

            public void setDesc(String desc) {
                this.desc = desc;
            }

            public List<String> getTags() {
                return tags;
            }

            public void setTags(List<String> tags) {
                this.tags = tags;
            }

            public AuthInfoBean getAuth_info() {
                return authinfobean;
            }


            public static class LocationBean {
                @SerializedName("lon")
                private double lon;
                @SerializedName("lat")
                private double lat;

                public double getLon() {
                    return lon;
                }
                public void setLon(double lon) {
                    this.lon = lon;
                }
                public double getLat() {
                    return lon;
                }
                public void setLat(double lat) {
                    this.lat = lat;
                }
            }


            public static class AuthInfoBean {
                private String SYS;

                public String getSYS() {
                    return SYS;
                }

                public void setSYS(String SYS) {
                    this.SYS = SYS;
                }
            }


        }
    }
}

#8


返回的字符串中统一格式啊,将  "auth_info": "2016072201" 这种 改成 
 "auth_info": {
                    "SYS": "Po1Cj1OK1v4ah5FZeckgNZ9q==8="
                },

这种不就好了,Gson是根据JavaBean来的,格式不统一 auth_info的数据怎么放进去,

#9


@moonFY 
但是平台返回的信息就存在这种同一字段不同类型的情况,况且平台不是自己搭建的。只允许接收。而且用jackson是可以解析出来的。这个应该不关格式不统一的问题吧 

#10


上面说了Gson是根据JavaBean解析的,JackJson没用过不知道具体情况

#11


auth_info定义为String就行了,然后再通过String 看能不能转化为AuthInfoBean,如果转化失败,则说明auth_info是一个字符串

#12


LZ怎么解决的,小弟也碰到了同一字段不同类型值得问题,还请赐教啊

#13


请问你是怎么解释出来的?我也遇到同样的问题

#14


用Object去接收;  

#15


引用 10 楼 moonFY 的回复:
上面说了Gson是根据JavaBean解析的,JackJson没用过不知道具体情况

怎么改啊

#1


JSONObject  auth_info =  JSONObject.getJSONObject("auth_info "); 
String sys  = auth_info .getString("SYS");

因为auth_info   也是一个 json 结构 获取 auth_info  信息的时候要用JSONObject 来接收  然后再获取 sys 的值
标红的 要改为你的变量 名

#2


@yang1912 org.json我已经解析出来了,目前我要的是基于Google的GSON解析。

#3


 "auth_info": {
                    "SYS": "cRDUW69=qBIMnlq=HtO3wtw65TI="
          },
 "auth_info": "3213230", 
解析不出吧
用gson 用的比较少
类似这种情况 就得自己写 解析规则 了 deserialize
用自己写的规则 去解析 反序列化  获得数据
实现 implements JsonDeserializer< >  标红是你的实体类

再具体 看下官方文档吧 挺全的
不好意思!

#4


Gson要的是格式统一吧。将格式不同 的那个改成统一的吧,需要用的时候做下判断呗

#5


@yang1912  用JACKSON可以简单地解析出来,目前已经做出来,而且可以判断,但是用GSON就在这一层出错啦。我也知道要用deserialize 但是目前没有可参考的Demo不知道兄弟有没有!有提供一下?

#6


@moonFY         Gson格式要怎样统一呢?还有定义的时候要怎么写呢?我都是直接使用GSON插件来生成的,调用过程中,总返回一个null, 在其它项也有出现返回null的情况,如private String routeTo;但是我后来在其上面添加@SerializedName("route_to") 就可以正常使用!

#7


这个是我那个Bean的初始代码:
帮忙看一下哪里出错啦


public class Bean{

    //@SerializedName("errno")
    private int errno;
  

    //@SerializedName("data")
    private DataBean data;
    //@SerializedName("error")
    private String error;

    public int getErrno() {
        return errno;
    }

    public void setErrno(int errno) {
        this.errno = errno;
    }

    public DataBean getData() {
        return data;
    }

    public void setData(DataBean data) {
        this.data = data;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public static class DataBean {
        @SerializedName("per_page")
        private int perPage;
        @SerializedName("total_count")
        private int totalCount;
        @SerializedName("page")
        private int page;
      

        //@SerializedName("devices")
        private List<DevicesBean> devices;

        public int getPerPage() {

            return perPage;
        }

        public void setPerPage(int perPage) {

            this.perPage = perPage;
        }

        public int getTotalCount() {

            return totalCount;
        }

        public void setTotalCount(int totalCount) {

            this.totalCount = totalCount;
        }

        public int getPage() {

            return page;
        }

        public void setPage(int page) {

            this.page = page;
        }

        public List<DevicesBean> getDevices() {

            return devices;
        }

        public void setDevices(List<DevicesBean> devices) {

            this.devices = devices;
        }

        public static class DevicesBean {
            //@SerializedName("private")
            private boolean privateX;
            @SerializedName("protocol")
            private String protocol;
            @SerializedName("route_to")
            private String routeTo;
            @SerializedName("create_time")
            private String createTime;
            //@SerializedName("online")
            private boolean online;
           

            @SerializedName("location")
            private LocationBean location;
            private AuthInfoBean authinfobean;






            //@SerializedName("auth_info")

            //@SerializedName("id")
            private String id;
            //@SerializedName("title")
            private String title;
            //@SerializedName("desc")
            private String desc;
            //@SerializedName("tags")
            private List<String> tags;


            public boolean isPrivateX() {

                return privateX;
            }

            public void setPrivateX(boolean privateX) {

                this.privateX = privateX;
            }

            public String getProtocol() {

                return protocol;
            }

            public void setProtocol(String protocol) {

                this.protocol = protocol;
            }

            public String getRouteTo() {

                return routeTo;
            }

            public void setRouteTo(String routeTo) {

                this.routeTo = routeTo;
            }

            public String getCreateTime() {

                return createTime;
            }

            public void setCreateTime(String createTime) {

                this.createTime = createTime;
            }

            public boolean isOnline() {

                return online;
            }

            public void setOnline(boolean online) {

                this.online = online;
            }

            public LocationBean getLocation() {

                return location;
            }

            public void setLocation(LocationBean location) {

                this.location = location;
            }





            public String getId() {
                return id;
            }

            public void setId(String id) {
                this.id = id;
            }

            public String getTitle() {
                return title;
            }

            public void setTitle(String title) {
                this.title = title;
            }

            public String getDesc() {
                return desc;
            }

            public void setDesc(String desc) {
                this.desc = desc;
            }

            public List<String> getTags() {
                return tags;
            }

            public void setTags(List<String> tags) {
                this.tags = tags;
            }

            public AuthInfoBean getAuth_info() {
                return authinfobean;
            }


            public static class LocationBean {
                @SerializedName("lon")
                private double lon;
                @SerializedName("lat")
                private double lat;

                public double getLon() {
                    return lon;
                }
                public void setLon(double lon) {
                    this.lon = lon;
                }
                public double getLat() {
                    return lon;
                }
                public void setLat(double lat) {
                    this.lat = lat;
                }
            }


            public static class AuthInfoBean {
                private String SYS;

                public String getSYS() {
                    return SYS;
                }

                public void setSYS(String SYS) {
                    this.SYS = SYS;
                }
            }


        }
    }
}

#8


返回的字符串中统一格式啊,将  "auth_info": "2016072201" 这种 改成 
 "auth_info": {
                    "SYS": "Po1Cj1OK1v4ah5FZeckgNZ9q==8="
                },

这种不就好了,Gson是根据JavaBean来的,格式不统一 auth_info的数据怎么放进去,

#9


@moonFY 
但是平台返回的信息就存在这种同一字段不同类型的情况,况且平台不是自己搭建的。只允许接收。而且用jackson是可以解析出来的。这个应该不关格式不统一的问题吧 

#10


上面说了Gson是根据JavaBean解析的,JackJson没用过不知道具体情况

#11


auth_info定义为String就行了,然后再通过String 看能不能转化为AuthInfoBean,如果转化失败,则说明auth_info是一个字符串

#12


LZ怎么解决的,小弟也碰到了同一字段不同类型值得问题,还请赐教啊

#13


请问你是怎么解释出来的?我也遇到同样的问题

#14


用Object去接收;  

#15


引用 10 楼 moonFY 的回复:
上面说了Gson是根据JavaBean解析的,JackJson没用过不知道具体情况

怎么改啊