Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InputStream to JsonObject - GSON

Tags:

java

android

gson

I've a lot of trouble converting a result from this api to a JsonObject.

The "result" stays null no matter what I try to do.

  URL url = new URL(urlString);   InputStream input = url.openStream();   Reader reader = new InputStreamReader(in, "UTF-8");   JsonResult result  = new Gson().fromJson(reader, JsonResult.class); 

JsonResult class

public class JsonResult {     private String status;     private Meta meta;     private ArrayList<Player> players; }  class Meta{     private String count; } 

Json:

{"status":"ok","meta":{"count":12},"data":[{"nickname":"DataBase","account_id":500566109},{"nickname":"database007","account_id":514382449},{"nickname":"Database04","account_id":504367425},{"nickname":"database08","account_id":515081772},{"nickname":"database1","account_id":503282284},{"nickname":"database1221","account_id":506709044},{"nickname":"database123","account_id":508409172},{"nickname":"database1337","account_id":501661259},{"nickname":"database169","account_id":503884400},{"nickname":"database2","account_id":504337382},{"nickname":"database93","account_id":518691821},{"nickname":"databaseking66","account_id":505911069}]} 

Note: This is for a school project

Note 2: I did check and test a lot of other solutions from SO but didn't find or understand the correct one.

Edit 1:

public class JsonResult {     @SerializedName("status")     public String status;     @SerializedName("meta")     public Meta meta;     @SerializedName("data")     public Player[] players; }  class Meta{     @SerializedName("count")     private String count; } 

Player class

public class Player {      private int account_id;     private String nickname;      public Player(int account_id, String nickname){         this.account_id = account_id;         this.nickname = nickname;     }       //Generated     public void setAccount_id(int account_id) {         this.account_id = account_id;     }      //Generated     public void setNickname(String nickname) {         this.nickname = nickname;     }      public String toString() {         return this.account_id + this.nickname;     } } 
like image 321
JochemQuery Avatar asked May 06 '15 19:05

JochemQuery


1 Answers

In your JsonResult class change

public class JsonResult {     private String status;     private Meta meta;     @SerializedName("data")     private ArrayList<Player> players; } 
like image 132
Ads Avatar answered Sep 25 '22 10:09

Ads