I try to retrieve a list of Object (ArrayList for instance) to a JSON
representation


For instance a sample User class:


 public class User {

    private String lastname;

    private String firstname;

    public User(){};
    public User(String lname,String fname){
        this.firstname = fname;
        this.lastname = lname;
    }

    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
 }


I want to retrieve a list of User like this


@GET @Path("users")
@Produces(MediaType.APPLICATION_JSON)
public ArrayList<User> getUsers() {     
    ArrayList<User> users = new ArrayList<User>();
    users.add(new User("userlastname1", "userfirstname1"));
    users.add(new User("userlastname3", "userfirstname2"));

    return users;
}


and I want to get this JSON representation


[
    {
        lastname: "userlastname1",
        firstname: "userfirstname1"
    },
    {
        lastname: "userlastname2",
        firstname: "userfirstname2"
    }
]


What is the best way to achieve this? 


The Restlet documentation mentioned the serialization proccess is automated,
yes it’s the case for only one Object like “User”


new User("userlastname1", "userfirstname1")


return me the good JSON representation


{
    lastname: "userlastname1",
    firstname: "userfirstname1" 
}


If some one can give me some explanation about that. much appreciated


Best regards


--
View this message in context: 
http://restlet-discuss.1400322.n2.nabble.com/Need-JAX-RS-JSON-Good-example-tp7290704p7290704.html
Sent from the Restlet Discuss mailing list archive at Nabble.com.

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2923428

Reply via email to