Parse JSON Array Response Using Retrofit & Gson

Parse JSON array response using Retrofit & Gson

Please try to use this one

    @FormUrlEncoded
@POST("api/sponsors")
Call<List<SponsorsResult>> getStatesAndDistrict(
@Field("xyz") String field1
);

Call <List<SponsorsResult>> call = service.getSponsorsValue();

call.enqueue(new Callback<List<SponsorsResult>>() {
@Override
public void onResponse(Call<List<SponsorsResult>> call, Response<List<SponsorsResult>> response) {

List<SponsorsResult> rs = response.body();

}

@Override
public void onFailure(Call<List<SponsorsResult>> call, Throwable t) {

}
});

class SponsorsResult {

@SerializedName("sponsors")
private List<SponsorsValue> sponsors;

public List<SponsorsValue> getSponsors() {
return sponsors;
}
}

class SponsorsValue{
@SerializedName("leg_id")
@Expose
private String legId;
@SerializedName("type")
@Expose
private String type;
@SerializedName("name")
@Expose
private String name;

public String getLegId() {
return legId;
}

public void setLegId(String legId) {
this.legId = legId;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

Please let me know if you are facing any issue.

How to parse json array of objects inside object using Retrofit

Try to use next class in response object:

data class PokedexResponse (
@SerializedName("pokemon")
val pokemons: List<Post>
)

interface SimpleApi {
@GET("pokedex.json")
suspend fun getCustomPosts(): Response<PokedexResponse>
}

My guess is that you missed to parse pokemon object:

{
"pokemon": [{ ... }]
}

How to parse json array using retrofit?

Your POJO class should be like this.

public class TempParams {
/**
* IsSuccess : true
* ResponseObject : ["one","two","three","four","five"]
*/

private boolean IsSuccess;
private List<String> ResponseObject;

public boolean isIsSuccess() {
return IsSuccess;
}

public void setIsSuccess(boolean IsSuccess) {
this.IsSuccess = IsSuccess;
}

public List<String> getResponseObject() {
return ResponseObject;
}

public void setResponseObject(List<String> ResponseObject) {
this.ResponseObject = ResponseObject;
}

}

After successfully getting response object you have to convert it like this.

Gson gson = new Gson();
TempParams model = gson.fromJson(mObject.toString(), TempParams.class);

Now you can get value from POJO class.

Parse Json array with multiple objects using retrofit 2 in android

Your Json key value data is changing from one index to another.

Wrong Json:

[{
//Make this either integer or boolean
"rank": 1,
// best rate is object here in the next index, it's treated as boolean.
"best_rate": {
"id_asset": "1",
"value": "5000",
"loan_to_value": "50",
"offered": "2500",
"annual_rate": "3",
"quantity": "5",
"rank": "1"
}

},
{
"rank":false,
"best_rate":false,
}
]

I have a complete project shared on github with your json.
https://github.com/lingarajsankaravelu/retrofit2v.git

Retrofit code should be like this as i mentioned in the question comment.

@GET("url")
Call<List<AssetResponse>> getAssetList();

As you have requested to explain the changes in your pojo class it should be like this.

AssetReponse.class:

public class AssetResponse {
private Integer id_asset;
private Integer id_category;
private Integer id_brand;
private String name;
private Integer status;
private String updated_at;

private AssetRate assetRate;
private AssetCategory assetCategory;
private Links links;
private Self self;
private AssetBrand assetBrand;
private HasLinked hasLinked;

public Integer getId_asset() {
return id_asset;
}

public void setId_asset(Integer id_asset) {
this.id_asset = id_asset;
}

public Integer getId_category() {
return id_category;
}

public void setId_category(Integer id_category) {
this.id_category = id_category;
}

public Integer getId_brand() {
return id_brand;
}

public void setId_brand(Integer id_brand) {
this.id_brand = id_brand;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getStatus() {
return status;
}

public void setStatus(Integer status) {
this.status = status;
}

public String getUpdated_at() {
return updated_at;
}

public void setUpdated_at(String updated_at) {
this.updated_at = updated_at;
}

public AssetRate getAssetRate() {
return assetRate;
}

public void setAssetRate(AssetRate assetRate) {
this.assetRate = assetRate;
}

public AssetCategory getAssetCategory() {
return assetCategory;
}

public void setAssetCategory(AssetCategory assetCategory) {
this.assetCategory = assetCategory;
}

public Links getLinks() {
return links;
}

public void setLinks(Links links) {
this.links = links;
}

public Self getSelf() {
return self;
}

public void setSelf(Self self) {
this.self = self;
}

public AssetBrand getAssetBrand() {
return assetBrand;
}

public void setAssetBrand(AssetBrand assetBrand) {
this.assetBrand = assetBrand;
}

public HasLinked getHasLinked() {
return hasLinked;
}

public void setHasLinked(HasLinked hasLinked) {
this.hasLinked = hasLinked;
}
}

AssetRate.class

public class AssetRate {

private Integer id_asset_rate;
private Integer id_asset;
private Double value;
private Double loan_to_value;
private Double offered;
private Double annual_rate;
private String updated_at;

public Integer getId_asset_rate() {
return id_asset_rate;
}

public void setId_asset_rate(Integer id_asset_rate) {
this.id_asset_rate = id_asset_rate;
}

public Integer getId_asset() {
return id_asset;
}

public void setId_asset(Integer id_asset) {
this.id_asset = id_asset;
}

public Double getValue() {
return value;
}

public void setValue(Double value) {
this.value = value;
}

public Double getLoan_to_value() {
return loan_to_value;
}

public void setLoan_to_value(Double loan_to_value) {
this.loan_to_value = loan_to_value;
}

public Double getOffered() {
return offered;
}

public void setOffered(Double offered) {
this.offered = offered;
}

public Double getAnnual_rate() {
return annual_rate;
}

public void setAnnual_rate(Double annual_rate) {
this.annual_rate = annual_rate;
}

public String getUpdated_at() {
return updated_at;
}

public void setUpdated_at(String updated_at) {
this.updated_at = updated_at;
}
}

Seperate your inner class like above. Seperating your inner class model will be useful if you are working on a large project. where you don't have to write the same pojo class again. you can use this seperate class structure instead.

How to grab JSON Array and use gson to parse each json object? (Retrofit)

I ended up just calling in the callback a list of the customObject and it did the job...

new Callback<List<ObjResponse>>() {

Retrofit/GSON Is not parsing my JSON array of object

Try add a wrapper for your parent class, like this:

public class ParentsWrapper {
@SerializeName("parent")
private Parent [] parents;
}

so your interface method should return a list of ParentsWapper objects, like so:

public interface DynamicMenuService {
@POST("http://dev.myurl.com/somepath")
Call<List<ParentsWrapper>> getMenuData();
}

You'll see that some of your classes are absolutely wrong, fix that too.

public class Parent
{
@SerializeName("title")
private String title;

@SerializeName("children")
private Children[] children;

public String getTitle ()
{
return title;
}

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

public Children[] getChildren ()
{
return children;
}

public void setChildren (Children[] children)
{
this.children = children;
}

}

public class Children
{
@SerializeName("category")
private Category category;

public Category getCategory ()
{
return category;
}

public void setCategory (Category category)
{
this.category = category;
}

}

public class Child
{
@SerializeName("title")
private String title;

@SerializeName("url")
private String url;

public String getTitle ()
{
return title;
}

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

public String getUrl ()
{
return url;
}

public void setUrl (String url)
{
this.url = url;
}

}

public class Category
{
@SerializeName("child")
private Child[] child;

@SerializeName("title")
private String title;

public Child[] getChild ()
{
return child;
}

public void setChild (Child[] child)
{
this.child = child;
}

public String getTitle ()
{
return title;
}

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

}


Related Topics



Leave a reply



Submit