0

I can't seem to wrap my head around how to setup my class hierarchy for JSON conversion using GSON.

My JSON looks like:

{
  "Users": {
    "id": 1,
    "name": "Jim",
    "location": "Huntsville"
  }
}

My User List class looks like:

public class UserList {
  public static List<User> Users;

  @SuppressWarnings("static-access")
  public void setUserList(List<User> userList){
      this.Users = userList;
  }

  public List<User> getUserList(){
      return Users;
  }

}

and lastly a user class that looks like this:

public class User {
  private int id;

  private String name;

  private String location;

  public int getId(){
      return id;
  }

  public String getName(){
      return name;
  }

  public String getLocation(){
      return location;
  }

  public String toString(){
      return("User: [id=" + id + "], [name=" + name + "], [location=" + location + "]");
  }

}

Anyone mind giving me a shove in the right direction? I'd appreciate it!

EDIT:

Forgot to show my parsing code.. (Just reading a sample JSON file from SDCard)

        BufferedReader br = new BufferedReader(new FileReader(Environment.getExternalStorageDirectory() + "/user.json"));
        UserList userList = gson.fromJson(br, UserList.class);
Nedlinin
  • 881
  • 2
  • 12
  • 21
  • See if the answer [here](http://stackoverflow.com/questions/8371274/how-to-parse-json-array-in-android-with-gson/8371455#8371455) helps you out. – yorkw May 17 '12 at 01:57
  • yorkw: I've tried that as well.. However, I end up with null values for all fields in the class. Also, the JSON will be expanded later to allow for multiple users to be returned at once (hence the list). – Nedlinin May 17 '12 at 02:06
  • First, you need setter method for all fields. Second, on server side, design your json in a unified pattern, for one user: [{user1: ...}], for multiple user: [{user1: ...}, {user2: ...}, ...]. – yorkw May 17 '12 at 02:12
  • Adding setters didn't help with GSON parsing.. And, server sided/JSON creation is not up to me. I have to tie into something already in place. – Nedlinin May 17 '12 at 02:48
  • @yorkw Gson doesn't need any setter methods, it directly sets the (non-final, even private) fields. I suggest not to have any setters that aren't needed by actual business logic (which Gson isn't). – Philipp Reichart May 21 '12 at 21:40
  • @Nedlinin Yorkw is right about your JSON being off. How will the JSON look like when it's "expanded later"? Right now there's no "list of users" anywhere in your JSON to be seen and mapping it now vs later will be quite different. – Philipp Reichart May 21 '12 at 21:42
  • @Nedlinin Also, there's no need for that `Users` field to be static. – Philipp Reichart May 21 '12 at 21:43

1 Answers1

1

are you sure your example JSON is correct? It does not seem to be a list of things, just one user is defined.

Furthermore, your getter and setters for Users, should be following the get/set pattern and be called

public List<User> getUsers()
public void setUsers(List<User> users)

Also, you can follow the Java convention of small case and instruct Gson to use a different casing. Assuming that you only have one entry of Users in your JSON. This would let you parse the snippit you provided, if you change the Users property into User not a list.

@SerializedName("Users")
private User user;

So if you want a list of users you should find that in the json, this should let you parse it as a list, !note that you need to have objects, which are enclosed, like:

{"users" : [{id:"one"}, ...]}

As pointed out in the comments.

Joey
  • 1,349
  • 14
  • 26