2016-11-02 3 views
0

У меня есть JsonObject со следующим содержанием:данные Синтаксического JSon с использованием Gson

{ 
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" 
} 

Я пытаюсь извлечь информацию адресной с 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; 
       } 
      } 
     } 
    } 
} 

Но когда Я пытаюсь получить доступ к информации с

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 дает мне parseMoto: null Некоторые советы? Заранее спасибо

ответ

1

Я нахожу окончательное решение. Это работает как шарм

    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()); 
      } 
Смежные вопросы