首页 > 代码库 > 使用Gson解析复杂、变态的Json数据

使用Gson解析复杂、变态的Json数据

接口文档:

app配置接口 client/init请求数据json={"uid":"","sid":"","ver":"1",    "request":{}}返回数据{"ret":0,    "response":{        "tag_category":{            "中国画":{                "年代":["先秦两汉","战国楚国","魏晋南北","隋唐五代","南宋北宋","元代","明清","近现代","年代不详","其他"],                "技法":["泼墨","工笔","写意","白描","写生","皴法","没骨","指头画","其他"],                ...            },            "书法":{                "分类":["书法","碑帖","写本写经","书札文牍","其他"],                "书体":["篆书","隶书","楷书","草书","行书","其他"],                ...            },            ...        }    }}备注 tag_category 标签分类

首先自然想到封装成JavaBean。

注意点:

1、内部嵌套的类必须是static的,要不然解析会出错;

2、类里面的属性名必须跟Json字段里面的Key一模一样;

3、因为Json字段里面的Key存在中文,所以必须在相应的字段上使用@SerializedName("中国画")注解,给Key取别名。

4、内部嵌套用{}嵌套的就定义为 public ChinaPicture chinaPicture,用[]括起来的部分是一个List,所以定义为 public List<String> years。

5、如果不想写get和set方法,只需要将字段定义成public。

public class JsonBean {    public int ret;    public Response response;    public static class Response {        public TagCategory tag_category;                public static class TagCategory {                        @SerializedName("中国画")            public ChinaPicture chinaPicture;            /**             * 中国画             */            public static class ChinaPicture {                @SerializedName("年代")                public List<String> years;                @SerializedName("技法")                public List<String> techniques;                @SerializedName("题材")                public List<String> topic;                @SerializedName("规格")                public List<String> specification;            }                        @SerializedName("书法")            public Calligraphy calligraphy;            /**             * 书法             */            public static class Calligraphy {                @SerializedName("分类")                public List<String> classification;                @SerializedName("书体")                public List<String> chirography;                @SerializedName("规格")                public List<String> specification;            }                        @SerializedName("西方绘画")            public WesternPicture westernPicture;            /**             * 西方绘画             */            public static class WesternPicture {                @SerializedName("技法")                public List<String> techniques;                @SerializedName("题材")                public List<String> topic;            }                        @SerializedName("邮品")            public Stamps stamps;            /**             * 邮品             */            public static class Stamps {                @SerializedName("分类")                public List<String> classification;                @SerializedName("规格")                public List<String> specification;            }                        @SerializedName("钱币")            public Money money;            /**             * 钱币             */            public static class Money {                @SerializedName("分类")                public List<String> classification;                @SerializedName("国家")                public List<String> country;                @SerializedName("规格")                public List<String> specification;            }                        @SerializedName("瓷器")            public China china;            /**             * 瓷器             */            public static class China {                @SerializedName("年代")                public List<String> years;                @SerializedName("分类")                public List<String> classification;            }                        @SerializedName("紫砂壶")            public Teapot teapot;            /**             * 紫砂壶             */            public static class Teapot {                @SerializedName("规格")                public List<String> specification;                @SerializedName("泥料")                public List<String> pug;                @SerializedName("工艺")                public List<String> crafts;                @SerializedName("行业")                public List<String> industry;            }                        @SerializedName("金银铜器")            public MetalTool metalTool;            /**             * 金银铜器             */            public static class MetalTool {                @SerializedName("年代")                public List<String> years;                @SerializedName("分类")                public List<String> classification;            }                        @SerializedName("木器家具")            public WoodFurniture woodFurniture;            /**             * 木器家具             */            public static class WoodFurniture {                @SerializedName("材质")                public List<String> material;                @SerializedName("器型")                public List<String> Shape;            }                        @SerializedName("玉石翡翠")            public Emerald emerald;            /**             * 玉石翡翠             */            public static class Emerald {                @SerializedName("材质")                public List<String> material;                @SerializedName("镶嵌")                public List<String> inlay;                @SerializedName("分类")                public List<String> classification;            }                        @SerializedName("红木文玩")            public RedWoodHeritage redWoodHeritage;            /**             * 红木文玩             */            public static class RedWoodHeritage {                @SerializedName("分类")                public List<String> classification;                @SerializedName("材质")                public List<String> material;            }                        @SerializedName("杂项")            public OtherItems otherItems;            /**             * 杂项             */            public static class OtherItems {                @SerializedName("年代")                public List<String> years;            }        }    }}

使用Gson解析复杂、变态的Json数据