[英]JSON objects in Retrofit 2.1 using Serialization
我正在對異步請求使用Retrofit庫,但是在此之前,我需要將Json轉換為Java對象。 我看過很少的教程,並且能夠理解。 然后我決定使用Nomadlist api自己做一個項目,json鏈接為https://nomadlist.com/api/v2/list/cities/mumbai-india/places/work
我很困惑如何使吸氣劑和setters對象
"updated":{"epoch":1473220041,"time":"2016-09-07T03:47:21+00:00","cache":false}
或從結果數組中如何獲取和獲取
"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;
}
}
我添加了我正在使用的Json部分的屏幕截圖。 城市國家和地區部分的正確格式是什么?
您可以使用以下格式來做有需要的事情。
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;
}
為City
, Country
和應用程序所需的任何其他JSONObject
創建單獨的類。 並且,在主模型類中創建其對象(我使用過Business)。
試試這種格式:
創建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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.