During the two days of the project, you need to convert an object into Jsonarray in action, and I'm using Jsonarray.fromobject. However, other string types of data are already available on the page side, which is a problem with the date type. With Firebug, the date is not exactly what you want it to be (originally wanted to put together the year, month and day display). Later, the Internet to find a lot of information, finally found a way. The Code in action is as follows:
Jsonconfig jsonconfig = new Jsonconfig (); Jsonconfig.registerjsonvalueprocessor (Date.class, New Jsondatevalueprocessor ()); Jsonarray = Jsonarray.fromobject (user,jsonconfig);//user for my object
Jsondatevalueprocessor is a Java class that handles 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'm here to turn the date into YYYY-MM-DD format.
JS in the code to get the properties of the object:
var RMC = data[0].birthday;//Birthday
Note: Refer to document Http://www.cnblogs.com/Nbge/archive/2012/07/31/2617127.html
Time display problem when using Jsonarray.fromobject when Java.util.Date is included in object