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:'[email protected]',
            friends:{
                user:{
                    name:'another name',
                    email:'[email protected]',
                    friends:{
                        user:{
                            name:'yet another name',
                            email:'[email protected]'
                        }
                    }
                }
            }
        }
    }
}

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);
10
  • 8
    That's XML, not JSON, mate. ...okay, you edited your question. Show us the JSON! Also, that's a constructor, not a class. Commented Mar 15, 2011 at 16:33
  • sorry having a nightmare with the code section lol Commented Mar 15, 2011 at 16:35
  • Read stackoverflow.com/editing-help. It's just Markdown. Commented Mar 15, 2011 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 Commented Mar 15, 2011 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 :) Commented Mar 15, 2011 at 16:38

1 Answer 1

44

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();
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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: 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.