简体   繁体   中英

how to convert json array to json object

I am using Retrofit 2 in my project my app get data from tow APIs api1 and 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"}]

and 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"}]

i want to make one POJO class for both APIs like

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

i try something like the above but it doesn't work for me i want to do like this so i can use one call function like this

    retrofit.enqueue(object: Callback<API_class> {

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

if the api1 and api2 was object Json like this

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

i will be able to do my idea but it's now impossible

If you want to transform your json array to a POJO you can use Gson dependency

and test this code below :

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

}

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

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