Gson 解析数据

下面是将要解析的 JSON 数据

{
        "resultcode": "200",
        "reason": "Success",
        "result": {
                "data": [{
                        "id": "14",
                        "title": "红烧肉",
                        "tags": "家常菜;老年人;咸;半小时-1小时;青少年;白领;红烧;1-2人;炒锅",
                        "imtro": "加了鹌鹑蛋,祝大家岁岁平“鹌”。",
                        "ingredients": "五花肉,250g;萝卜,100g;鹌鹑蛋,20个",
                        "burden": "油,适量;盐,适量",
                        "albums": ["http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/t\/0\/14_359794.jpg"],
                        "steps": [{
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_706ca81e0bbecefe.jpg",
                                "step": "1.带皮五花肉 ,鹌鹑蛋,白萝卜备用。"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_dc89a7e8f2823202.jpg",
                                "step": "2.配料备用。"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_ae5efcd7698c7a51.jpg",
                                "step": "3.鹌鹑蛋煮熟后去皮,备用。"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_6246ae9cde95a443.jpg",
                                "step": "4.白萝卜洗净,切大块。"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_fa36e18ba88ba812.jpg",
                                "step": "5.五花肉,洗净切大块。"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_fb6017cd9ea66698.jpg",
                                "step": "6.焯水后备用。"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_79b1cc9fb722b4dd.jpg",
                                "step": "7.油锅烧热,下入肉块。"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_0a45336653b0eb33.jpg",
                                "step": "8.煎到微黄。"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_cb483431930bb8cd.jpg",
                                "step": "9.加入白糖。"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_93eababb3d6f328a.jpg",
                                "step": "10.炒到金黄色。"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_b1672001decab1dc.jpg",
                                "step": "11.加入料酒和老抽,炒均匀。"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_03ce88cb45776456.jpg",
                                "step": "12.放入干红椒,生姜, 八角, 香叶,花椒,炒出香味后,后加入适量的水。"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_5e067f35b2297a47.jpg",
                                "step": "13.煮十几分钟后,加入萝卜和鹌鹑蛋。"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/14_7f8a56f9b8498703.jpg",
                                "step": "14.再煮上十几分钟,肉和萝卜软烂,大火收汁加入适量的盐即可。"
                        }]
                }, {
                        "id": "45",
                        "title": "秘制红烧肉",
                        "tags": "家常菜;热菜;烧;煎;炖;红烧;炒锅",
                        "imtro": "做红烧肉的豆亲们很多,大家对红烧肉的热爱更不用我说,从名字上就能反映出来。一些高手们对红烧肉的认识更是令我佩服,单单就红烧肉的做法、菜谱都看得我是眼花缭乱,口水横流。单纯的红烧肉我平时还真没做过,再不抓紧时间做一回解解馋,不是对不起别人,而是太对不起我自己了! 这道菜的菜名用了秘制二字来形容,当然是自己根据自己多年吃货的经验想象出来的,我不介意把自己的做法与大家共享,只为大家能同我一样,吃到不同口味的红烧肉。不同的人们根据自己的习惯都有不同的做法,味道也不尽相同。我的秘制的关键就是必须用玫瑰腐乳、冰糖和米醋这三种食材,腐乳和冰糖可以使烧出来的肉色泽红亮,米醋能解腻,令肥肉肥而不腻,此法烧制的红烧肉软糯中略带咸甜,的确回味无穷!",
                        "ingredients": "五花肉,500g",
                        "burden": "玫瑰腐乳,适量;盐,适量;八角,适量;草果,适量;香叶,适量;料酒,适量;米醋,适量;生姜,适量",
                        "albums": ["http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/t\/0\/45_854851.jpg"],
                        "steps": [{
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/45_0824e37faf00b71e.jpg",
                                "step": "1.将五花肉煮至断生状态"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/45_b6d7329b703f6e85.jpg",
                                "step": "2.切成大小一致的块"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/45_6ee9e8dab0516333.jpg",
                                "step": "3.放在锅内煎"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/45_b9afd6d4dd81f55c.jpg",
                                "step": "4.入生姜"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/45_d0170fbe236421f9.jpg",
                                "step": "5.放八角草果各一个,香叶一片"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/45_639b12210745fa41.jpg",
                                "step": "6.放冰糖"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/45_c25e0cedd2012f45.jpg",
                                "step": "7.加料酒"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/45_eb68327980f022dd.jpg",
                                "step": "8.加玫瑰腐乳和腐乳汁及适量盐"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/45_ac17263a11507a41.jpg",
                                "step": "9.加米醋"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/45_f5489af5d12b4930.jpg",
                                "step": "10.加水继续炖"
                        }, {
                                "img": "http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com\/cookbook\/s\/1\/45_8e0cf83cb7306281.jpg",
                                "step": "11.直至肉变软糯汤汁收干即可"
                        }]
                }],
                "totalNum": "306",
                "pn": 0,
                "rn": "2"
        },
        "error_code": 0
}

我们先创建实体类  这里我用的 GsonFormat 插件 一键创建。

实体类代码:

ublic class ResponseBean {
    private String resultcode;
    private String reason;
    private ResultBean result;
    private int error_code;

    public String getResultcode() {
        return resultcode;
    }

    public void setResultcode(String resultcode) {
        this.resultcode = resultcode;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public ResultBean getResult() {
        return result;
    }

    public void setResult(ResultBean result) {
        this.result = result;
    }

    public int getError_code() {
        return error_code;
    }

    public void setError_code(int error_code) {
        this.error_code = error_code;
    }

    public static class ResultBean {
        private String totalNum;
        private int pn;
        private String rn;
        private List<DataBean> data;

        public String getTotalNum() {
            return totalNum;
        }

        public void setTotalNum(String totalNum) {
            this.totalNum = totalNum;
        }

        public int getPn() {
            return pn;
        }

        public void setPn(int pn) {
            this.pn = pn;
        }

        public String getRn() {
            return rn;
        }

        public void setRn(String rn) {
            this.rn = rn;
        }

        public List<DataBean> getData() {
            return data;
        }

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

        public static class DataBean {
            private String id;
            private String title;
            private String tags;
            private String imtro;
            private String ingredients;
            private String burden;
            private List<String> albums;
            private List<StepsBean> steps;

            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 getTags() {
                return tags;
            }

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

            public String getImtro() {
                return imtro;
            }

            public void setImtro(String imtro) {
                this.imtro = imtro;
            }

            public String getIngredients() {
                return ingredients;
            }

            public void setIngredients(String ingredients) {
                this.ingredients = ingredients;
            }

            public String getBurden() {
                return burden;
            }

            public void setBurden(String burden) {
                this.burden = burden;
            }

            public List<String> getAlbums() {
                return albums;
            }

            public void setAlbums(List<String> albums) {
                this.albums = albums;
            }

            public List<StepsBean> getSteps() {
                return steps;
            }

            public void setSteps(List<StepsBean> steps) {
                this.steps = steps;
            }

            public static class StepsBean {

                private String img;
                private String step;

                public String getImg() {
                    return img;
                }

                public void setImg(String img) {
                    this.img = img;
                }

                public String getStep() {
                    return step;
                }

                public void setStep(String step) {
                    this.step = step;
                }
            }
        }
    }
}

实体类创建好了,开始解析数据

 Gson gson=new Gson();
                    String  responseData=response.body().string();
                    ResponseBean responseBean=gson.fromJson(responseData,ResponseBean.class);
                    ResponseBean.ResultBean resultBean=responseBean.getResult();
                    final List<ResponseBean.ResultBean.DataBean> dataBeanList=  resultBean.getData();
                    for (ResponseBean.ResultBean.DataBean dataBean:dataBeanList){
                        String title=dataBean.getTitle();
                        String imtro=dataBean.getImtro();
                        String ingredients=dataBean.getIngredients();
                        String burden=dataBean.getBurden();
                        List<String> albums=dataBean.getAlbums();
                        stepsBeanList= dataBean.getSteps();
                        for (ResponseBean.ResultBean.DataBean.StepsBean stepsBean:stepsBeanList){
                            String img=stepsBean.getImg();
                            String step=stepsBean.getStep();
                        }
                    }

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值