Spring Web MVC Json - jackson key/name for array of objects (named array) -
how json, containing java objects, key identifier of array? have following in spring mvc 4.x. use jackson library marshal objects json.
@requestmapping(value="dogs") public list<dog> getdogs(){ list<dog> list_dogs = new arraylist<dog>(); list_dogs.add(new dog("dog1",1)); list_dogs.add(new dog("dog2",2)); return list_dogs; }
i following response: [{"name":"dog1","age":1},{"name":"dog2","age":2}]
i have following:
{ "array": [{ "name": "dog1", "age": 1 }, { "name": "dog2", "age": 2 }] }
how provide name array?
ok, use map instead of list.
@requestmapping(value="dogs") public map<string, list<dog>> getdogs(){ map<string, list<dog>> map = new hashmap<string, list<dog>>(); list<dog> list_dogs = new arraylist<dog>(); list_dogs.add(new dog("dog1",1)); list_dogs.add(new dog("dog2",2)); map.put("myarray", list_dogs); //return list_dogs; [{"name":"dog1","age":1},{"name":"dog2","age":2}] return map; //{"myarray":[{"name":"dog1","age":1},{"name":"dog2","age":2}]} } {"myarray":[{"name":"dog1","age":1},{"name":"dog2","age":2}]}
Comments
Post a Comment