Reputation: 2609
I'm using GSON library to map JSON response from API and map to an object.
Here is my URL link:
https://hacker-news.firebaseio.com/v0/item/8863.json?print=pretty
And my model class TopStory
public class TopStory implements Comparable<TopStory> {
int id;
String title;
String by;
int score;
List<Integer> kids;
long time;
String url;
public TopStory() {
}
public TopStory(int id, String title, String by, int score, long time,String url,List kids) {
this.id = id;
this.title = title;
this.by = by;
this.score = score;
this.time = time;
this.url = url;
this.kids = kids;
}
and i'm create a function to create an object from JSON response:
public static TopStory fromJson(JSONObject jsonObject) {
TopStory topStory = new TopStory();
Gson gson = new Gson();
topStory = gson.fromJson(jsonObject.toString(),TopStory.class);
I'm having problem with the field kids. from Response it's an JSONARRAY
. how can we deserialize and map to and object field List
. And how me exclude to manually Deserialize and assign to specific field?
Upvotes: 0
Views: 255
Reputation: 9009
Actually kids
is not a List
, it's an Array
, so declare kids
like this
Integer[] kids;
Upvotes: 1
Reputation: 8939
Try using
Gson gson = new GsonBuilder().create();
TopStory topStory = gson.fromJson(jsonObject.toString(),TopStory.class);
List<Integer> kids;
// Correct declaration
Upvotes: 0
Reputation: 344
You can use Retrofit to make api calls et create a serializable model to get datas.
Upvotes: 0