Koptan
Koptan

Reputation: 145

Consuming Json in Play Framework

I try to run this code but I get a Null Exception.

Java Code :

public static void updateData(List<Users> users){ 
   for(Users u : users){ //Error 
      System.out.println(u.name); // Error 
   } 
}

Extjs Code :

proxy: { 
    type: 'ajax', 
    api: { 
        update: '/Application/updateData' 
    }, 
    reader: { 
        type: 'json', 
        root: 'users', 
        successProperty: 'success' 
    } 
  }

Json Array :

[{"name":"Ed","email":"[email protected]"},{"name":"Ez","email":"[email protected]"}] 

So please tell how to bind json Array to Entity List on Play Framework 1.2.2. Thanks ...

Upvotes: 1

Views: 1039

Answers (2)

kheraud
kheraud

Reputation: 5288

You have to use Gson :

List<User> userList = new Gson().fromJson(yourString, Users.class);

And have a Users class suitable for your JSON :

public class Users {
    private String name;
    private String email;
    ...
    //[Add your getter and setter]
    ...
}

For more information you can read the GSON documentation

Upvotes: 0

Molecular Man
Molecular Man

Reputation: 22386

You've specified root: 'users' in your reader's config. This means that JSON Array should look like this:

{users: [{"name":"Ed","email":"[email protected]"},{"name":"Ez","email":"[email protected]"}]}

Upvotes: 2

Related Questions