I have a rest controller which returns JSON in some format but I want the JSON output in a different format .To implement the same I am using Spring REST.I have posted all model and controller classes below.My question is what could I do to get my expected response?
ACTUAL JSON
[
{
"name": "CategoryName1",
"sub": [
{
"name": "SubCategory1"
},
{
"name": "SubCategory2"
}
]
},
{
"name": "CategoryName2",
"sub": [
{
"name": "SubCategory3"
},
{
"name": "SubCategory4"
}
]
}
]
EXPECTED JSON
{
"CategoryName1": ["SubCategory1", "SubCategory2"],
"CategoryName2": ["SubCategory1", "SubCategory2"]
}
Category
public class Category{
public String name;
public List<Subcategory> sub;
public List<Subcategory> getSub() {
return sub;
}
public void setSub(List<Subcategory> sub) {
this.sub = sub;
}
public Category(String name) {
super();
this.name = name;
}
}
Subcategory
public class Subcategory{
public String name;
public Subcategory(String name) {
super();
this.name = name;
}
}
Controller
@RequestMapping(value = "/dropdown", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody List<Category> dropdown() {
Subcategory SubCategory1 = new Subcategory("SubCategory1");
Subcategory SubCategory2 = new Subcategory("SubCategory2");
Subcategory SubCategory3 = new Subcategory("SubCategory3");
Subcategory SubCategory4 = new Subcategory("SubCategory4");
Category CategoryName1 = new Category("CategoryName1");
Category CategoryName2 = new Category("CategoryName2");
List<Subcategory> subList = new ArrayList<Subcategory>();
subList.add(SubCategory1);
subList.add(SubCategory2);
List<Subcategory> subList2 = new ArrayList<Subcategory>();
subList2.add(SubCategory3);
subList2.add(SubCategory4);
CategoryName1.setSub(subList);
CategoryName2.setSub(subList2);
List<Category> cat = new ArrayList<Category>();
cat.add(CategoryName1);
cat.add(CategoryName2);
return cat;
}