29

I am trying to load my Json into my class

public User() {
    this.fbId = 0;
    this.email = "";
    this.name = "";
    this.thumb = "";
    this.gender = "";
    this.location = "";
    this.relationship = null;
    this.friends = new ArrayList();
}
{
    users:{
        user:{
            name:'the name',
            email:'some@email.com',
            friends:{
                user:{
                    name:'another name',
                    email:'this@email.com',
                    friends:{
                        user:{
                            name:'yet another name',
                            email:'another@email.com'
                        }
                    }
                }
            }
        }
    }
}

I am struggling to get GSON to load the user details into the above Java object with the following code

User user = gson.fromJson(this.json, User.class);
Nithin
  • 748
  • 1
  • 10
  • 27
Deviland
  • 3,324
  • 7
  • 32
  • 53
  • 8
    That's XML, not JSON, mate. ...okay, you edited your question. Show us the JSON! _Also_, that's a constructor, not a class. – Matt Ball Mar 15 '11 at 16:33
  • sorry having a nightmare with the code section lol – Deviland Mar 15 '11 at 16:35
  • Read http://stackoverflow.com/editing-help. It's just Markdown. – Matt Ball Mar 15 '11 at 16:36
  • right i'm their never had problems like that before lol sorry guys thanks for the quick replies and sorry for messing you about – Deviland Mar 15 '11 at 16:37
  • its the constructor from the class with all the attributes of the class, thought it might be helpful to match the Json with the class in the example :) – Deviland Mar 15 '11 at 16:38
  • Could you provide more details on what your problems are? – Thomas Mar 15 '11 at 16:40
  • no problem when I use fromJson I am getting a new User but the attributes from the JSOn are not being assigned, I read the documentation for GSON and was led to believe this was automatic? – Deviland Mar 15 '11 at 16:41
  • ...okay, so what's the problem? Your JSON is basically an empty skeleton. Does the code throw exceptions? What happens/doesn't happen that shouldn't/should? – Matt Ball Mar 15 '11 at 16:42
  • Yes the actual code is a lot longer this is a cut down version the actual json has values within them, I tried to get the example to be simple and have not put values in sorry. but I get a new User object that has an ID of 0 which is within the constructor but nothing else – Deviland Mar 15 '11 at 16:43
  • what's the error/exception you are getting – Akintayo Olusegun Mar 15 '11 at 16:40

1 Answers1

43

The JSON is invalid. A collection is not to be represented by {}. It stands for an object. A collection/array is to be represented by [] with commaseparated objects.

Here's how the JSON should look like:

{
    users:[{
        name: "name1",
        email: "email1",
        friends:[{
            name: "name2",
            email: "email2",
            friends:[{
                name: "name3",
                email: "email3"
            },
            {
                name: "name4",
                email: "email4"
            }]
        }]
    }]
}

(note that I added one more friend to the deepest nested friend, so that you understand how to specify multiple objects in a collection)

Given this JSON, your wrapper class should look like this:

public class Data {
    private List<User> users;
    // +getters/setters
}

public class User {
    private String name;
    private String email;
    private List<User> friends;
    // +getters/setters
}

and then to convert it, use

Data data = gson.fromJson(this.json, Data.class);

and to get the users, use

List<User> users = data.getUsers();
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks Balus C you I used a tool to convert my test data from XML to JSON this is a lesson I have to learn don't trust third party sites to do the work I should be doing. – Deviland Mar 15 '11 at 16:55
  • Hi everyone used this in solution: gson.fromJson(this.json, User.class); I want to know whats this.json. Is it a json object or what? if it is a hard-coded json object then where should we create that object to access it with this.json? – Piscean Jun 14 '11 at 11:39
  • 1
    @Piscean: it's just the input JSON string. See also the original question and the Gson manual. If you can't figure it, just press `Ask Question` button on right top. – BalusC Jun 14 '11 at 11:45