[英]JSON objects in Retrofit 2.1 using Serialization
I am using Retrofit Library for Async request But before that I need to convert Json into java objects. 我正在对异步请求使用Retrofit库,但是在此之前,我需要将Json转换为Java对象。 I have seen few tutorials and was able to understand. 我看过很少的教程,并且能够理解。 I then decided to do a project on my own using Nomadlist api, the json link is https://nomadlist.com/api/v2/list/cities/mumbai-india/places/work 然后我决定使用Nomadlist api自己做一个项目,json链接为https://nomadlist.com/api/v2/list/cities/mumbai-india/places/work
I am confused how to make getters and setters object 我很困惑如何使吸气剂和setters对象
"updated":{"epoch":1473220041,"time":"2016-09-07T03:47:21+00:00","cache":false}
or from the result array how to make getters and setter for 或从结果数组中如何获取和获取
"city":{"name":"Thane","slug":"thane-india","url":"\/thane-india"}.
I have made the the following class. 我已经完成了以下课程。
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;
}
}
I have added the screenshot of the portion of Json I am using. 我添加了我正在使用的Json部分的屏幕截图。 What would be the correct format for city country and location section. 城市国家和地区部分的正确格式是什么?
You could use following format to do the needful. 您可以使用以下格式来做有需要的事情。
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;
}
Create separate class for City
, Country
and any other JSONObject
you need for your application. 为City
, Country
和应用程序所需的任何其他JSONObject
创建单独的类。 And, create its object in the main model class (I used Business). 并且,在主模型类中创建其对象(我使用过Business)。
Try this format : 试试这种格式:
create POJO JsonResponse.Class 创建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 {
}
create POJO class Result.class 创建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;
}
}
create POJO class Updated.class 创建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.