Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gson and the argonauts - problem converting javascript array, to json string, to java pojo using gson. Trying to get my structures correct

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 ;
    }
like image 325
NimChimpsky Avatar asked Nov 14 '22 07:11

NimChimpsky


1 Answers

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

like image 94
Jeroen Rosenberg Avatar answered Nov 17 '22 06:11

Jeroen Rosenberg