views:

89

answers:

1

Here is my json string, that I am acessing in java:

json = 
[
    {"id":"123456","Data":"skill2","dmlcrud":false},
    {"id":"123456","Data":"skill3","dmlcrud":true},
    {"id":"123456","Data":"skill14","dmlcrud":true},
    {"id":"123321","Data":"skill1","dmlcrud":false},
    {"id":"123321","Data":"skill14","dmlcrud":false}
]

I now want to put it in a collection so ideally/theoretically I would want to do:

List<Person> personList = new Gson().fromJson(json, Person.class);

and personList.size() would = 5. I would then loop through personList and preform my relevant actions.

However, my understanding is that I would need to create a container class, which itself contains the person list ? So instead of (public getters/setters removed for brevity, probably syntax errror in there aswell).

Class Person {
   private integer id;
   private String Data;
   private Boolean dmlCrud ;
}

I would actually need something like ?

Class container{
   List<Person> personList;


   static class Person {
      private integer id;
      private String Data;
      private Boolean dmlCrud ;
   }
}

However I would then need to alter the javascript json to be somethign different aswell ? Which seems rather problematic as am I creating the json string from a javascript array, using JSON.stringifier.

Any help gratefully received.

EDIT

the solution I used was to add

public List<Person> personList;

to the person class and alter the json object so that it was

{ "personList" :
    [
        {"id":"123456","Data":"skill2","dmlcrud":false},
        {"id":"123456","Data":"skill3","dmlcrud":true},
        {"id":"123456","Data":"skill14","dmlcrud":true},
        {"id":"123321","Data":"skill1","dmlcrud":false},
        {"id":"123321","Data":"skill14","dmlcrud":false}
    ]
}

the gson call can then be

Person person = new Gson().fromJson(json, Person.class);

and the data accessed in a list like so

List<Person> personList = person.getPersonList();

EDIT 2

A second, better, solution is to use this json array

[
        {"id":"123456","Data":"skill2","dmlcrud":false},
        {"id":"123456","Data":"skill3","dmlcrud":true},
        {"id":"123456","Data":"skill14","dmlcrud":true},
        {"id":"123321","Data":"skill1","dmlcrud":false},
        {"id":"123321","Data":"skill14","dmlcrud":false}
]

and then use

Type listType = new TypeToken<List<SkillsGsonTO>>() {}.getType();
List<Person> personList  = new Gson().fromJson(json,listType);
Person person1 = personList.get(0);

where the original class is used

Class Person {
       private integer id;
       private String Data;
       private Boolean dmlCrud ;
    }
A: 

You could use a Container class but this only makes sense if you need to ship additional properties on the person list. If this is not the case, you could convert to a java.util.List as well. I think you need to specify the "name" of the list property as a root element in your JSON string. So for instance if you're domain object is a List of Person objects, than your JSON root element is: "persons" or "personList". So you're JSON could look something like:

"persons" : {[
     {"id":"123456","Data":"skill2","dmlcrud":false},
     {"id":"123456","Data":"skill3","dmlcrud":true},
     {"id":"123456","Data":"skill14","dmlcrud":true},
     {"id":"123321","Data":"skill1","dmlcrud":false},
     {"id":"123321","Data":"skill14","dmlcrud":false}
]}

I could be a little bit off with the syntax, but it should be something similar to this. So to summarize:

In your case you can leave you're Person class untouched and gson should be able to create the List persons for you from the JSON string I suggested.

From the Gson API docs: If the object that your are deserializing is a ParameterizedType (i.e. contains at least one type parameter and may be an array) then you must use the fromJson(String, Type) method. Here is an example for deserialing a ParameterizedType:

 Type listType = new TypeToken<List<String>>() {}.getType();

 Gson gson = new Gson();
 List<String> target2 = gson.fromJson(json, listType);

So in your case it would be:

Type listType = new TypeToken<List<Person>>() {}.getType();
List<Person> persons = new Gson().fromJson(json, listType);

where json is your json string obviously

Jeroen Rosenberg
I don't see how the list would get created. I agree the json object structure needs to change as you suggested. But I must declare a list somewhere in either person or container... ?
NimChimpsky
I've editted my answer to be more specific. In your case, you could use Type listType = new TypeToken<List<Person>>() {}.getType(); and get the List<Person> reference with a call to new Gson().fromJson(json, listType), where json is your json string. I hope this clarifies it.
Jeroen Rosenberg
ah ok, that looks a bit neater than my edit, I will give it a try. Thanks. What exactly is this "Type listType = new TypeToken<List<Person>>() {}.getType();" doing then ?
NimChimpsky
It will get the underlying type instance for the generic type see http://google-gson.googlecode.com/svn/tags/1.5/docs/javadocs/index.html
Jeroen Rosenberg
with your suggestion the extra person in json string is not required, it takes the json array. I will update my question
NimChimpsky
I never seen this syntax before : " {}.getType(); " can you provide any further explanation of whats going on?
NimChimpsky
TypeToken is an abstract class. So basically, you're creating an anonymous class and call the default implementation for getType() on it.
Jeroen Rosenberg