{ meta: { limit: 0, offset: 0, total_count: 49 }, objects: [ { category: "Певец/Певица", created_at: "2016-07-04T11:27:49.050021", description: "hccjcj ", email: "xcjcu@hh.ru", extra_phone_number: "8582838", id: 64, name: "гаггсгссгссгсгсгсгсшмшмшп", phone_number: "688838383838388383", photo_url: "", price: 6868, resource_uri: "/api/post/64/", surname: "осоммомгп", updated_at: "2016-07-04T11:27:49.050069", video_url: null }, { category: "Тамада", created_at: "2016-07-04T08:03:58.411222", description: "gsgvs", email: "sgha@ejs.com", extra_phone_number: "5465", id: 63, name: "ysuhhs", phone_number: "0123456789", photo_url: "", price: 1235, resource_uri: "/api/post/63/", surname: "gsjeg", updated_at: "2016-07-04T08:03:58.411287", video_url: null }, 

This json needs to be converted into objects of class Person. these objects are grouped into categories.

 Data list = new Gson().fromJson(json,Data.class); return list.getList().size(); 

issues a NullPointerExeption.

 public class Data implements Serializable{ private List<Person> list; public void setList(List<Person> list) { this.list = list; } public List<Person> getList() { return list; } } 

Person class

 public class Person implements Serializable { String name; String surname; String category; String price; String bitmap; String description; String region; String created_at; String email; String phone_number; String video_url; String id; + setters & getters } 
  • and what is your problem exactly? Or do you want someone to write you ready code? - Vladyslav Matviienko
  • Data list = new Gson (). FromJson (json, Data.class); list is empty. public class Data implements Serializable {private List <Person> list; public void setList (List <Person> list) {this.list = list; } public List <Person> getList () {return list; }} - cherry
  • retrakt the question and insert your code there. - Vladyslav Matviienko
  • And you should read a lesson on GSON, because you have no idea how to work with it. - Vladyslav Matviienko

1 answer 1

Gson deserializes json by property names, i.e. to get the objects you have in Data need the objects property, not the list . Therefore either

 public class Data implements Serializable { List<Person> objects; } 

, or (more flexible method) specify from which json property to deserialize into your list :

 public class Data implements Serializable{ @SerializedName("objects") List<Person> list; }