简体   繁体   English

使用Gson解析Json数据

[英]Parsing Json data using Gson

I have a JsonObject with the following content: 我有一个具有以下内容的JsonObject

    {
id: "6705",
title: "Moto1",
address: {
location: [
{
meta_key: "map_lat",
meta_value: "1.4567"
},
{
meta_key: "map_lng",
meta_value: "2.345"
}
]
},
price: "25",
sale: "25",
number: "1",
image: "http://example.com"
}

I'm trying to extract address's info with Gson library I have the following Class 我正在尝试使用Gson库提取地址信息,我有以下课程

    public class MotoClass {

    private List<MotoBean> Moto;

    public List<MotoBean> getMoto() {
        return Moto;
    }

    public void setMoto(List<MotoBean> Moto) {
        this.Moto = Moto;
    }

    public static class MotoBean {
        private String id;
        private String title;
        private AddressBean address;
        private String price;
        private String sale;
        private String number;
        private String image;

        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 AddressBean getAddress() {
            return address;
        }

        public void setAddress(AddressBean address) {
            this.address = address;
        }

        public String getPrice() {
            return price;
        }

        public void setPrice(String price) {
            this.price = price;
        }

        public String getSale() {
            return sale;
        }

        public void setSale(String sale) {
            this.sale = sale;
        }

        public String getNumber() {
            return number;
        }

        public void setNumber(String number) {
            this.number = number;
        }

        public String getImage() {
            return image;
        }

        public void setImage(String image) {
            this.image = image;
        }

        public static class AddressBean {


            private List<LocationBean> location;

            public List<LocationBean> getLocation() {
                return location;
            }

            public void setLocation(List<LocationBean> location) {
                this.location = location;
            }

            public static class LocationBean {
                private String meta_key;
                private String meta_value;

                public String getMeta_key() {
                    return meta_key;
                }

                public void setMeta_key(String meta_key) {
                    this.meta_key = meta_key;
                }

                public String getMeta_value() {
                    return meta_value;
                }

                public void setMeta_value(String meta_value) {
                    this.meta_value = meta_value;
                }
            }
        }
    }
}

But when I try to access to info with 但是当我尝试使用以下方式访问信息时

Gson gson = new GsonBuilder().create();
Type collectionType = new TypeToken<Collection<MotoClass.MotoBean.AddressBean>>(){}.getType();
Collection<MotoClass.MotoBean.AddressBean> enums = gson.fromJson(response,collectionType);
MotoClass.MotoBean.AddressBean[] result = enums.toArray(new MotoClass.MotoBean.AddressBean[enums.size()]);
Log.d(TASK, String.valueOf(motoClass.getLocation()));

log gives me parseMoto: null Some advice ? log给我parseMoto: null一些建议? Thanks in advance 提前致谢

I find the final solution. 我找到了最终的解决方案。 It works like a charm 它像一个魅力

                 Gson gson = new Gson();
            MotoClass moto = gson.fromJson(responseStr,MotoClass.class);
         for (int i = 0; i<moto.getMoto().size();i++){
     Log.d(TAG,"onSuccess:EqL["+i+"]"+moto.getMoto().get(i).getTitle());
          }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM