2016-09-07 84 views
0

我使用Retrofit庫進行異步請求但在此之前,我需要將Json轉換爲java對象。我看過很少的教程,並且能夠理解。於是我決定做一個項目,我自己使用Nomadlist API,JSON的鏈接是https://nomadlist.com/api/v2/list/cities/mumbai-india/places/work使用序列化在Retrofit 2.1中的JSON對象

我很困惑如何使getter和setter對象

"updated":{"epoch":1473220041,"time":"2016-09-07T03:47:21+00:00","cache":false} 

或從結果數組如何使getter和setter for

"city":{"name":"Thane","slug":"thane-india","url":"\/thane-india"}. 

我已經做了以下類。

public class City { 

@SerializedName("name") 
private String nameNmd; 

public String getNameNmd() { 
    return nameNmd; 
} 


@SerializedName("img") 
private String imgNmd; 

public String getImgNmd() { 
    return imgNmd; 
} 

@SerializedName("url") 
private String urlNmd; 

public String getUrlNmd() { 
    return urlNmd; 
} 

@SerializedName("type") 
private String typeNmd; 

public String getTypeNmd() { 
    return typeNmd; 
} 
} 

enter image description here

我已經加入我使用JSON的部分的屏幕截圖。城市國家和地區部分的正確格式是什麼?

+0

嘗試[jsonschema2pojo](http://www.jsonschema2pojo.org /)直接從json創建Java對象 – Rehan

回答

0

您可以使用以下格式來做必要的事情。

public class City { 
    @SerializedName("name") 
    String name; 
    @SerializedName("slug") 
    String slug; 
    @SerializedName("url") 
    String url; 
} 

public class Country { 

} 

public class Business { 
    @SerializedName("name") 
    String name; 
    @SerializedName("img") 
    String img; 
    @SerializedName("url") 
    String url; 
    @SerializedName("type") 
    String type; 
    @SerializedName("city") 
    City city; 
    @SerializedName("country") 
    Country country; 
} 

CityCountry和任何其他創建單獨的類JSONObject你需要爲你的應用程序。並且,在主模型類(我使用Business)中創建它的對象。

0

試試這個格式:

創建POJO JsonResponse.Class

public class res { 
     @SerializedName("name") 
     String name; 
     @SerializedName("img") 
     String img; 
     @SerializedName("url") 
     String url; 
     @SerializedName("type") 
     String type; 

     @SerializedName("country") 
     Country country; 

     public String getName() { 
      return name; 
     } 

     public void setName(String name) { 
      this.name = name; 
     } 

     public String getImg() { 
      return img; 
     } 

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

     public String getUrl() { 
      return url; 
     } 

     public void setUrl(String url) { 
      this.url = url; 
     } 

     public String getType() { 
      return type; 
     } 

     public void setType(String type) { 
      this.type = type; 
     } 

     public Country getCountry() { 
      return country; 
     } 

     public void setCountry(Country country) { 
      this.country = country; 
     } 
    } 

    public class Country { 

    } 

創建POJO類Result.class

 public class Result{ 

    @SerializedName("name") 
    String name; 
    @SerializedName("img") 
    String img; 
    @SerializedName("url") 
    String url; 
    @SerializedName("type") 
    String type; 
    @SerializedName("result") 
    ArrayList<Result> result; 

    @SerializedName("country") 
    Country country; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getImg() { 
     return img; 
    } 

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

    public String getUrl() { 
     return url; 
    } 

    public void setUrl(String url) { 
     this.url = url; 
    } 

    public String getType() { 
     return type; 
    } 

    public void setType(String type) { 
     this.type = type; 
    } 

    public ArrayList<Result> getResult() { 
     return result; 
    } 

    public void setResult(ArrayList<Result> result) { 
     this.result = result; 
    } 

    public Country getCountry() { 
     return country; 
    } 

    public void setCountry(Country country) { 
     this.country = country; 


} 
} 

創建POJO類Updated.class

public class Updated{ 
     @SerializedName("epoch") 
     String epoch; 
     @SerializedName("time") 
     String time; 
     @SerializedName("ache") 
     String ache; 
    public String getEpoch() { 
    return epoch; 
} 

public void setEpoch(String epoch) { 
    this.epoch = epoch; 
} 

public String getTime() { 
    return time; 
} 

public void setTime(String time) { 
    this.time = time; 
} 

public String getAche() { 
    return ache; 
} 

public void setAche(String ache) { 
    this.ache = ache; 
} 

    } 
0

enter link description here過去的迴應,並選擇源類型:Json和註釋樣式註釋樣式:在右邊,提供包和類名擊中預覽最後你會得到你模型類