Use Gson to parse complex and abnormal Json data (including Chinese key)

Keywords: JSON Attribute

Original address: http://www.cnblogs.com/bianmajiang/p/3998083.html

Interface documentation:
Request data

json={"uid":"","sid":"","ver":"1",
     "request":{}
    }
  • 1
  • 2
  • 3

Return data

{"ret":0,
    "response":{
        "tag_category":{
            "Chinese Painting":{
                "years":["Pre Qin and Han Dynasties","Chu state in the Warring States Period","Wei, Jin, South and North","Sui, Tang and Five Dynasties","Southern Song and Northern Song","Yuan dynasty","Ming and Qing Dynasties","Modern times","sine anno","Other"],
                "technique":["Splashing ink","Fine brushwork","Freehand brushwork","Line drawing","sketch","Texturing methods","Mogu","finger painting","Other"],
                ...
            },
            "Calligraphy":{
                "classification":["Calligraphy","Tablet inscription","To write a book","Letters and documents","Other"],
                "Book style":["Seal character","Official script","Regular script","Cursive script","Running script","Other"],
                ...
            },
            ...
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

The first natural thought is to encapsulate JavaBean s.

Note:

1. The inner nested class must be static, or the parsing will fail;

2, as like as two peas in the Json field, the attribute names in the class must be exactly the same as those in the Key field.

3. Because the Key in the Json field exists in Chinese, you must use @ SerializedName("Chinese painting") annotation on the corresponding field to alias the Key.

4. The part nested with {} is defined as public chinapicture, and the part enclosed with [] is a List, so it is defined as public List years.
5. If you don't want to write get and set methods, just define the fields as public.

public class JsonBean {
    public int ret;
    public Response response;

    public static class Response {
        public TagCategory tag_category;

        public static class TagCategory {

            @SerializedName("Chinese Painting")
            public ChinaPicture chinaPicture;
            /**
             * Chinese Painting
             */
            public static class ChinaPicture {
                @SerializedName("years")
                public List<String> years;
                @SerializedName("technique")
                public List<String> techniques;
                @SerializedName("theme")
                public List<String> topic;
                @SerializedName("Specifications")
                public List<String> specification;
            }

            @SerializedName("Calligraphy")
            public Calligraphy calligraphy;
            /**
             * Calligraphy
             */
            public static class Calligraphy {
                @SerializedName("classification")
                public List<String> classification;
                @SerializedName("Book style")
                public List<String> chirography;
                @SerializedName("Specifications")
                public List<String> specification;
            }

            @SerializedName("Western painting")
            public WesternPicture westernPicture;
            /**
             * Western painting
             */
            public static class WesternPicture {
                @SerializedName("technique")
                public List<String> techniques;
                @SerializedName("theme")
                public List<String> topic;
            }

            @SerializedName("Postal products")
            public Stamps stamps;
            /**
             * Postal products
             */
            public static class Stamps {
                @SerializedName("classification")
                public List<String> classification;
                @SerializedName("Specifications")
                public List<String> specification;
            }

            @SerializedName("Coin")
            public Money money;
            /**
             * Coin
             */
            public static class Money {
                @SerializedName("classification")
                public List<String> classification;
                @SerializedName("Country")
                public List<String> country;
                @SerializedName("Specifications")
                public List<String> specification;
            }

            @SerializedName("Porcelain")
            public China china;
            /**
             * Porcelain
             */
            public static class China {
                @SerializedName("years")
                public List<String> years;
                @SerializedName("classification")
                public List<String> classification;
            }

            @SerializedName("dark-red enameled pottery teapot")
            public Teapot teapot;
            /**
             * dark-red enameled pottery teapot
             */
            public static class Teapot {
                @SerializedName("Specifications")
                public List<String> specification;
                @SerializedName("Slurry")
                public List<String> pug;
                @SerializedName("Technology")
                public List<String> crafts;
                @SerializedName("industry")
                public List<String> industry;
            }

            @SerializedName("Gold, silver and copper ware")
            public MetalTool metalTool;
            /**
             * Gold, silver and copper ware
             */
            public static class MetalTool {
                @SerializedName("years")
                public List<String> years;
                @SerializedName("classification")
                public List<String> classification;
            }

            @SerializedName("Wood furniture")
            public WoodFurniture woodFurniture;
            /**
             * Wood furniture
             */
            public static class WoodFurniture {
                @SerializedName("Texture of material")
                public List<String> material;
                @SerializedName("Device type")
                public List<String> Shape;
            }

            @SerializedName("Jade and jadeite")
            public Emerald emerald;
            /**
             * Jade and jadeite
             */
            public static class Emerald {
                @SerializedName("Texture of material")
                public List<String> material;
                @SerializedName("Set")
                public List<String> inlay;
                @SerializedName("classification")
                public List<String> classification;
            }

            @SerializedName("Mahogany games")
            public RedWoodHeritage redWoodHeritage;
            /**
             * Mahogany games
             */
            public static class RedWoodHeritage {
                @SerializedName("classification")
                public List<String> classification;
                @SerializedName("Texture of material")
                public List<String> material;
            }

            @SerializedName("miscellaneous")
            public OtherItems otherItems;
            /**
             * miscellaneous
             */
            public static class OtherItems {
                @SerializedName("years")
                public List<String> years;
            }
        }
    }
}

Posted by ultimachris on Fri, 01 May 2020 08:50:32 -0700