英文:
sql mapping to json object
问题
以下是您提供的JSON对象的中文翻译:
{
"year":"2020",
"month":[
{
"name":"一月",
"month":"1",
"payroll_dates":[
"1",
"2"
],
"holiday_dates":[
"3",
"4"
]
},
{
"name":"二月",
"month":"2",
"payroll_dates":[
"1",
"2"
],
"holiday_dates":[
"3",
"4"
]
},
{
"name":"三月",
"month":"3",
"payroll_dates":[
"1",
"2"
],
"holiday_dates":[
"3",
"4"
]
},
{
"name":"四月",
"month":"4",
"payroll_dates":[
"1",
"2"
],
"holiday_dates":[
"3",
"4"
]
},
{
"name":"五月",
"month":"5",
"payroll_dates":[
"1",
"2"
],
"holiday_dates":[
"3",
"4"
]
}
]
}
数据库设计中的内容不需要翻译。
Java代码部分的内容不需要翻译。
英文:
I am looking for a better approach of mapping my query to json object.
Structure of json object is -
{
"year":"2020",
"month":[
{
"name":"January",
"month":"1",
"payroll_dates":[
"1",
"2"
],
"holiday_dates":[
"3",
"4"
]
},
{
"name":"Feburary",
"month":"2",
"payroll_dates":[
"1",
"2"
],
"holiday_dates":[
"3",
"4"
]
},
{
"name":"March",
"month":"3",
"payroll_dates":[
"1",
"2"
],
"holiday_dates":[
"3",
"4"
]
},
{
"name":"April",
"month":"4",
"payroll_dates":[
"1",
"2"
],
"holiday_dates":[
"3",
"4"
]
},
{
"name":"May",
"month":"5",
"payroll_dates":[
"1",
"2"
],
"holiday_dates":[
"3",
"4"
]
}
]
}
and database design is :
id | calendarDt | year | month | day | calendarType
1. | 2020-1-25 | 2020 | 1 | 25 | Holiday
2 | 2020-1-22 | 2020 | 1 | 22 | Holiday
3 | 2020-1-13 | 2020 | 1 | 13 | Payday
These are the details of january month for holiday and payday.
The java code i have used is :
created a list for each month for holiday.
Created a list for each month for payroll.
Created an object for each month.
And then set the list in the month object.
Is there a better approach for doing it in java?
List<String> payrollStringJan= new ArrayList<String>();
List<String> holidayStringJan = new ArrayList<String>();
Month janMonth = new Month();
for (Calendar item: payrollCalendarList) {
switch(item.getMonth()) {
case 1:
populateMonthObject(item, payrollStringJan,
holidayStringJan);
break;
}
private void populateMonthObject(UhgCalendar item,
List<String> payrollString, List<String> holidayString) {
if("Payday".equals(item.getCalendarType())) {
payrollString.add(Integer.toString(item.getDay()));
}
if("Holiday".equals(item.getCalendarType())) {
holidayString.add(Integer.toString(item.getDay()));
}
}
Json pojo -
public class PayrollCalendar {
@JsonProperty("year")
private String year;
@JsonProperty("month")
private List<Month> month = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
//getter setter follow
}
public class Month {
@JsonProperty("name")
private String name;
@JsonProperty("month")
private String month;
@JsonProperty("payroll_dates")
private List<String> payrollDates = null;
@JsonProperty("holiday_dates")
private List<String> holidayDates = null;
//getter setter follow
}
}
答案1
得分: 0
首先,我想了解为什么数据库的设计是这样的。数据库只需要包含日历日期和日历类型。如果您想要在某个特定月份、几个月或一年中查找假期/发薪日,您可以轻松编写查询。
就 JSON 对象而言,如果客户端希望获取所有日期,它可以简单地包含一个发薪日数组和一个假期数组,我认为对于这种用例,响应的大小不会太大。
英文:
First of all, I want to understand why the database is designed so. The database can just have calendar date and calendar type. If you want the holiday/Payday in a given month or few months or in a year, you can write the query easily.
In terms of Json object, it can simply have a payday array and a holiday array if the client wants all the dates and I don't think response will be too big for this use-case.
专注分享java语言的经验与见解,让所有开发者获益!
评论