I want to parse JSON arrays and use gson. Firstly, I can log JSON output, the server responds clearly to the client.
Here is my JSON output:
[ { id : '1', title: 'sample title', .... }, { id : '2', title: 'sample title', .... }, ... ]
I tried this structure for parsing. A class that depends on a single array and ArrayList for all JSONArray.
public class PostEntity { private ArrayList<Post> postList = new ArrayList<Post>(); public List<Post> getPostList() { return postList; } public void setPostList(List<Post> postList) { this.postList = (ArrayList<Post>)postList; } }
Publication Class:
public class Post { private String id; private String title; }
When I try to use gson without errors, there are no warnings and no logs:
GsonBuilder gsonb = new GsonBuilder(); Gson gson = gsonb.create(); PostEntity postEnt; JSONObject jsonObj = new JSONObject(jsonOutput); postEnt = gson.fromJson(jsonObj.toString(), PostEntity.class); Log.d("postLog", postEnt.getPostList().get(0).getId());
What is wrong, how can I decide?
java json android arrays gson
Ogulcan Orhan Dec 03 2018-11-12T00: 00Z
source share