简体   繁体   中英

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. 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

I am confused how to make getters and setters object

"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. 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. And, create its object in the main model class (I used Business).

Try this format :

create 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

       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

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;
}

    }

响应后转到此处输入链接描述,然后选择源类型:Json和批注样式批注样式:在右侧,提供包和类名称,然后单击“预览”,最后得到模型类

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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