繁体   English   中英

Retrofit 2.1中使用序列化的JSON对象

[英]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;
}

CityCountry和应用程序所需的任何其他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;
}

    }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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