The time displayed when JSONArray. fromObject is used when the object contains java. util. Date.

Source: Internet
Author: User

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

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.