The time displayed when JSONArray. fromObject is used when the object contains java. util. Date.
During the two days of Project creation, an object needs to be converted into a JSONArray in the action. I am using JSONArray. fromObject. However, data of other String types has been obtained on the page, that is, there is a problem with the Date type. According to firebug, the date is not exactly what you want (originally we wanted to splice year, month, and day ). Later, I found some information on the Internet and finally found a method. The code in action is as follows:
JsonConfig jsonConfig = new JsonConfig (); jsonConfig. Values (Date. class, new JsonDateValueProcessor (); jsonArray = JSONArray. fromObject (user, jsonConfig); // user is my object
JsonDateValueProcessor is a Java class for processing dates:
public class JsonDateValueProcessor implements JsonValueProcessor { private String format =yyyy-MM-dd; public Object processArrayValue(Object value, JsonConfig config) { return process(value); } 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,Locale.UK); return sdf.format(value); } return value == null ? : value.toString(); } }
Note: I want to convert the date to yyyy-MM-dd format.
Code for retrieving object attributes in js:
Var rmc = data [0]. birthday; // birthday