如何将json数组转换为json对象

时间:2017-11-17 14:07:53

标签: java android json kotlin retrofit

我在我的项目中使用Retrofit 2 我的应用程序从两个API api1和api2

获取数据

api1: -

[{"id":"29867","category_id":"31","author":"admins",
    "title":"hi","abstract":"","image_name":"","created":"11","body":"hi"},
    {"id":"29866","category_id":"14","author":"admins",
    "title":"hi2","abstract":"","image_name":"img.png","created":"2017-11-16 16:02:00","body":"heloo"}]

和api2: -

    [{"obj":"29867","category_id":"31","obj2":"admins",
"title":"hi","abstract":"","image_name":"","created":"11","obj3":"hi"},
{"obj":"29866","category_id":"14","author":"admins",
"obj2":"hi2","abstract":"","image_name":"Img.png","created":"2017-11-16 16:02:00","obj3":"Hello"}]

我想为两个API制作一个POJO类,如

class API_class{
var list:List<api1> =null
var list2:List<pi2> =null

}

class api1{
 @SerializedName("id")
    @Expose
    var id: Int? = null
.
.
.

}

class api2{
@SerializedName("obj")
    @Expose
    var obj: Int? = null
.
.
.
}

我尝试了类似上面的内容,但它并不适合我 我想这样做,所以我可以使用一个呼叫功能 像这样

    retrofit.enqueue(object: Callback<API_class> {

            override fun onResponse(call: Call<API_class>?, response: Response<API_class>?) {.
.
.
.
}

如果api1和api2是像这样的对象Json

{json:"ddd"
[.....]
}

我将能够实现我的想法,但现在已经不可能了

1 个答案:

答案 0 :(得分:0)

如果要将json数组转换为POJO,可以使用Gson依赖

并测试以下代码:

    public static void main(String[] args) throws IOException {


    String json = "[{\"id\":\"29867\",\"category_id\":\"31\",\"author\":\"admins\",\n" +
            "    \"title\":\"hi\",\"abstract\":\"\",\"image_name\":\"\",\"created\":\"11\",\"body\":\"hi\"},\n" +
            "    {\"id\":\"29866\",\"category_id\":\"14\",\"author\":\"admins\",\n" +
            "     \"title\":\"hi2\",\"abstract\":\"\",\"image_name\":\"img.png\",\"created\":\"2017-11-16 16:02:00\",\"body\":\"heloo\"}]";


    Gson gson = new Gson();
    Example yourObj[] = gson.fromJson(json, Example[].class);

}

示例POJO:

   @JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "id",
        "category_id",
        "author",
        "title",
        "abstract",
        "image_name",
        "created",
        "body"
})
public class Example {
    @JsonProperty("id")
    private String id;
    @JsonProperty("category_id")
    private String categoryId;
    @JsonProperty("author")
    private String author;
    @JsonProperty("title")
    private String title;
    @JsonProperty("abstract")
    private String _abstract;
    @JsonProperty("image_name")
    private String imageName;
    @JsonProperty("created")
    private String created;
    @JsonProperty("body")
    private String body;

    @JsonProperty("id")
    public String getId() {
        return id;
    }

    @JsonProperty("id")
    public void setId(String id) {
        this.id = id;
    }

    @JsonProperty("category_id")
    public String getCategoryId() {
        return categoryId;
    }

    @JsonProperty("category_id")
    public void setCategoryId(String categoryId) {
        this.categoryId = categoryId;
    }

    @JsonProperty("author")
    public String getAuthor() {
        return author;
    }

    @JsonProperty("author")
    public void setAuthor(String author) {
        this.author = author;
    }

    @JsonProperty("title")
    public String getTitle() {
        return title;
    }

    @JsonProperty("title")
    public void setTitle(String title) {
        this.title = title;
    }

    @JsonProperty("abstract")
    public String getAbstract() {
        return _abstract;
    }

    @JsonProperty("abstract")
    public void setAbstract(String _abstract) {
        this._abstract = _abstract;
    }

    @JsonProperty("image_name")
    public String getImageName() {
        return imageName;
    }

    @JsonProperty("image_name")
    public void setImageName(String imageName) {
        this.imageName = imageName;
    }

    @JsonProperty("created")
    public String getCreated() {
        return created;
    }

    @JsonProperty("created")
    public void setCreated(String created) {
        this.created = created;
    }

    @JsonProperty("body")
    public String getBody() {
        return body;
    }

    @JsonProperty("body")
    public void setBody(String body) {
        this.body = body;
    }
}