{
"code": 200,
"message": "Success",
"data": [
{
"holidayId": 1,
"createdAt": 1334925720000,
"date": "2012-01-26",
"description": "Republic Day",
"holidayType": {
"holidayTypeId": 1,
"holidayType": "Normal"
}
},
{
"holidayId": 11,
"createdAt": 1334925720000,
"date": "2012-03-23",
"description": "Ugadi",
"holidayType": {
"holidayTypeId": 2,
"holidayType": "Normal"
}
},
{
"holidayId": 21,
"createdAt": 1334925720000,
"date": "2012-04-06",
"description": "Good Friday",
"holidayType": {
"holidayTypeId": 1,
"holidayType": "Normal"
}
},
{
"holidayId": 31,
"createdAt": 1334925720000,
"date": "2012-05-01",
"description": "May Day",
"holidayType": {
"holidayTypeId": 2,
"holidayType": "Normal"
}
},
}
]
}
这是我当前的JSON格式:
public CommonResponseModel getLeaveList() {
EntityManager entityManager = DBManager.getDBManager();
List<Holiday> result = entityManager.createQuery("FROM Holiday", Holiday.class).getResultList();
LeavesResponseWrapper model = new LeavesResponseWrapper();
model.setData(result);
DBManager.saveAndClosDB(entityManager);
return model;
}
此代码我正在尝试获取当前的JSON数据
但是我必须以这种格式获取JSON数据:
{
"code": 200,
"message": "Holiday List",
"data": {
"normalHolidays": [
{
"name": "Diwali",
"date": "31-10-2016"
}
],
"optionalHolidays": [
{
"name": "Onam",
"date": "13-09-2016"
}
]
}
}
请帮助我如何实现期望的JSON格式的数据,我们有两个表,一个假期,另一个用于Holidays类型,其依据是我正在获取数据,而我必须为TYPE1到json数组一个为type2另一个我正在使用Jersey罐子请帮助我怎么得到它。
最佳答案
将“数据”的类型从List<Holiday>
更改为Map<String,List<Holiday>>
。
然后,根据holidayType,您可以将setData()
中的数据分开。
Map<String,List<Holiday>> data;
setData(List<Holiday> holidays) {
List<Holiday> normalHolidays = new ArrayList<Holiday>();
List<Holiday> optionalHolidays = new ArrayList<Holiday>();
for(Holiday holiday : holidays) {
if("Normal".equals(holiday.holidayType.holidayType)) {
normalHolidays.add(holiday);
} else {
optionalHolidays.add(holiday);
}
}
data = new HashMap<String,List<Holiday>>();
data.put("normalHolidays", normalHolidays);
data.put("optionalHolidays", optionalHolidays);
}