java Data object 用 JSONObject.fromObject 或 JSONArray.fromObject做轉換的時候,
會轉成一個[ object object]的模式,如下:
"itemDateFrom":{"date":18,"hours":0,"seconds":0,"month":8,"nanos":0,"timezoneOffset":-480,"year":119,"minutes":0,"time":1568736000000,"day":3}
因為和預期 ( YYYY/MM/DD ) 不同,可以加入自定義轉換方式
- 先自定一個轉換定義
public class DateValue implements JsonValueProcessor{ private String format ="yyyy/MM/dd"; public DateValue(){ super(); } public DateValue(String datePattern){ super(); this.format = datePattern; } @Override public Object processArrayValue(Object value, JsonConfig config) { return process(value); } @Override public Object processObjectValue(String key, Object value, JsonConfig config) { return process(value); } private Object process(Object value){ if(value instanceof Date){ SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(value); } return value == null ? "" : value.toString(); } }
- 進行轉換
JsonConfig jsonConfig = new JsonConfig(); jsonConfig.registerJsonValueProcessor(Date.class , new DateValue()); //jsonConfig.registerJsonValueProcessor(Date.class , new DateValue("yyyy-MM-dd")); JSONArray.fromObject(rtnProductUsableVoList,jsonConfig);
文章標籤
全站熱搜
留言列表