1
{ "records": [ { "id": "744", "categoryname": "Floor Tiles", "photo": "", "width": "0", "height": "0", "secondlevelcategory": [ { "id": "833", "categoryname": "Digital Vitrified Tiles", "photo": "http://image.com", "width": "1031", "height": "700" } ] }, { "id": "744", "categoryname": "Floor Tiles", "photo": "", "width": "0", "height": "0", "secondlevelcategory": [ { "id": "833", "categoryname": "Digital Vitrified Tiles", "photo": "http://image.com", "width": "1031", "height": "700" } ] } ] } 

How can i Convert this Json response to Retrofit bean i am getting the Gson Error like Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

private void callCategoryApi() { Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(GithubUserAPI.CATEGORY_API) .addConverterFactory(GsonConverterFactory.create(gson)) .build(); GithubUserAPI githubUserAPI = retrofit.create(GithubUserAPI.class); Call<List<CatResponseData>> call = githubUserAPI.getCategory(Utils.key); call.enqueue(this); } @Override public void onResponse(Call<List<CatResponseData>> call, Response<List<CatResponseData>> response) { int code = response.code(); if (code == 200) { List<CatResponseData> res = response.body(); for(CatResponseData resDat : res){ System.out.println("=== Response : " + resDat.getCategoryname()); } }else{ Toast.makeText(this, "Did not work: " + String.valueOf(code), Toast.LENGTH_LONG).show(); } } @Override public void onFailure(Call<List<CatResponseData>> call, Throwable t) { System.out.println("=== Error : " + t.getMessage()); } 

and api call is

@POST("/apikey/{apikey}") Call<List<CatResponseData>> getCategory(@Path("apikey") String user); 

String CATEGORY_API = "https://api.callingservice.com";

please help me to solve this issue How can i Convert Json Response To Bean and My Bean class is like

public class CategoryBean { List<CatResponseData> responseData = new ArrayList<CatResponseData>(); } class CatResponseData { String id; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getCategoryname() { return categoryname; } public void setCategoryname(String categoryname) { this.categoryname = categoryname; } String categoryname; } 

3 Answers 3

1

This should be the class that you give to Retrofit. Not the list of beans, because they are encapsulated in an object.

 public class CategoryResponse { @SerializedName("records") List<CategoryBean> responseData; public List<CatResponseData> getResponseData() { return responseData; } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Hi toshkinl Thanks for the response, after creating the CategoryResponse class then how can i access id,categoryname,height,secondlevelcategory and how can i call the api, please guide me , thanks
First, this is how your bean should look like pastebin.com/fqx3k1KH. Then when you enqueue the call
0

sorry I can't comment but I want to help; Can't be that retrofit is waiting just for array of items without key name? I mean, maybe you need to tell Json to obtain the object "records" before iterate it

In my retrofit Im recieving json array like: [{},{},{}]

instead: {"records": [{},{},{}]}

Comments

0

To convert, Gson needs the same name on class fields. I recommend use that site, you can paste you JSON and he returns the class in Gson format

For each List you have, you need a class with the objects. like :

Main class:

 import java.util.ArrayList; import java.util.List; import javax.annotation.Generated; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; @Generated("org.jsonschema2pojo") public class Example { @SerializedName("records") @Expose private List<Record> records = new ArrayList<Record>(); /** * No args constructor for use in serialization * */ public Example() { } /** * * @param records */ public Example(List<Record> records) { this.records = records; } /** * * @return * The records */ public List<Record> getRecords() { return records; } /** * * @param records * The records */ public void setRecords(List<Record> records) { this.records = records; } public Example withRecords(List<Record> records) { this.records = records; return this; } } 

Record.java

package com.example; import java.util.ArrayList; import java.util.List; import javax.annotation.Generated; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; @Generated("org.jsonschema2pojo") public class Record { @SerializedName("id") @Expose private String id; @SerializedName("categoryname") @Expose private String categoryname; @SerializedName("photo") @Expose private String photo; @SerializedName("width") @Expose private String width; @SerializedName("height") @Expose private String height; @SerializedName("secondlevelcategory") @Expose private List<Secondlevelcategory> secondlevelcategory = new ArrayList<Secondlevelcategory>(); /** * No args constructor for use in serialization * */ public Record() { } /** * * @param id * @param secondlevelcategory * @param height * @param width * @param categoryname * @param photo */ public Record(String id, String categoryname, String photo, String width, String height, List<Secondlevelcategory> secondlevelcategory) { this.id = id; this.categoryname = categoryname; this.photo = photo; this.width = width; this.height = height; this.secondlevelcategory = secondlevelcategory; } /** * * @return * The id */ public String getId() { return id; } /** * * @param id * The id */ public void setId(String id) { this.id = id; } public Record withId(String id) { this.id = id; return this; } /** * * @return * The categoryname */ public String getCategoryname() { return categoryname; } /** * * @param categoryname * The categoryname */ public void setCategoryname(String categoryname) { this.categoryname = categoryname; } public Record withCategoryname(String categoryname) { this.categoryname = categoryname; return this; } /** * * @return * The photo */ public String getPhoto() { return photo; } /** * * @param photo * The photo */ public void setPhoto(String photo) { this.photo = photo; } public Record withPhoto(String photo) { this.photo = photo; return this; } /** * * @return * The width */ public String getWidth() { return width; } /** * * @param width * The width */ public void setWidth(String width) { this.width = width; } public Record withWidth(String width) { this.width = width; return this; } /** * * @return * The height */ public String getHeight() { return height; } /** * * @param height * The height */ public void setHeight(String height) { this.height = height; } public Record withHeight(String height) { this.height = height; return this; } /** * * @return * The secondlevelcategory */ public List<Secondlevelcategory> getSecondlevelcategory() { return secondlevelcategory; } /** * * @param secondlevelcategory * The secondlevelcategory */ public void setSecondlevelcategory(List<Secondlevelcategory> secondlevelcategory) { this.secondlevelcategory = secondlevelcategory; } public Record withSecondlevelcategory(List<Secondlevelcategory> secondlevelcategory) { this.secondlevelcategory = secondlevelcategory; return this; } } 

Secondlevelcategory.java

package com.example; import javax.annotation.Generated; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; @Generated("org.jsonschema2pojo") public class Secondlevelcategory { @SerializedName("id") @Expose private String id; @SerializedName("categoryname") @Expose private String categoryname; @SerializedName("photo") @Expose private String photo; @SerializedName("width") @Expose private String width; @SerializedName("height") @Expose private String height; /** * No args constructor for use in serialization * */ public Secondlevelcategory() { } /** * * @param id * @param height * @param width * @param categoryname * @param photo */ public Secondlevelcategory(String id, String categoryname, String photo, String width, String height) { this.id = id; this.categoryname = categoryname; this.photo = photo; this.width = width; this.height = height; } /** * * @return * The id */ public String getId() { return id; } /** * * @param id * The id */ public void setId(String id) { this.id = id; } public Secondlevelcategory withId(String id) { this.id = id; return this; } /** * * @return * The categoryname */ public String getCategoryname() { return categoryname; } /** * * @param categoryname * The categoryname */ public void setCategoryname(String categoryname) { this.categoryname = categoryname; } public Secondlevelcategory withCategoryname(String categoryname) { this.categoryname = categoryname; return this; } /** * * @return * The photo */ public String getPhoto() { return photo; } /** * * @param photo * The photo */ public void setPhoto(String photo) { this.photo = photo; } public Secondlevelcategory withPhoto(String photo) { this.photo = photo; return this; } /** * * @return * The width */ public String getWidth() { return width; } /** * * @param width * The width */ public void setWidth(String width) { this.width = width; } public Secondlevelcategory withWidth(String width) { this.width = width; return this; } /** * * @return * The height */ public String getHeight() { return height; } /** * * @param height * The height */ public void setHeight(String height) { this.height = height; } public Secondlevelcategory withHeight(String height) { this.height = height; return this; } } 

2 Comments

Hi Lucas Thanks for the response, i just create a pojo class as per u says and i call api like @POST("/apikey/{apikey}") Call<Example> getCategory(@Path("apikey") String user); can u say where i doing wrong because i have getting still error like Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $
on your onResponse, you have changed the class correct ? Before it was a List of objects, now it's just one object (with a list of others objects inside him)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.