jsonapi-converter icon indicating copy to clipboard operation
jsonapi-converter copied to clipboard

Help: Is there any options to parse this json? Generic type.

Open ExtinctAmoeba opened this issue 5 years ago • 2 comments

Json: https://jsonblob.com/96555bba-be78-11e9-bd8b-cdbff37ad6cb

It has generic type and I cannot find any solution for this issue.

ExtinctAmoeba avatar Aug 14 '19 09:08 ExtinctAmoeba

@jasminb Can You please help me with this?

ExtinctAmoeba avatar Aug 20 '19 07:08 ExtinctAmoeba

Hello @ExtinctAmoeba, this can be achieved in following way:

Write a base class like this (having in mind that attributes are similar in both sub-types, your base class can hold all of the common attributes, below is a just simple example):

public class BaseModel {

	@Id
	private String id;

	public String getId() {
		return id;
	}

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

And than write two different classes for each of your types (Movie and Vod):

@Type("Movie")
public class Movie extends BaseModel {
	private Integer categoryId;

	public Integer getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}
}
@Type("Vod")
public class Vod extends BaseModel {

	private Integer categoryId;

	public Integer getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}
}

Make sure to register both types with ResourceConverter instance.

To handle response, do:

JSONAPIDocument<List<BaseModel>> elements = converter.readDocumentCollection(data, BaseModel.class);

jasminb avatar Aug 20 '19 08:08 jasminb