SPRINGMVC JSON Date Object Conversion __json

Source: Internet
Author: User
Tags dateformat
In spring MVC3, for example, when doing rest, you often have to parse with Jason and encounter the problem of date formatting.
Take a look at the example:

1 For example, there is a Pojo
Package Com.loiane.model;

Import Java.util.Date;

Import Org.codehaus.jackson.annotate.JsonAutoDetect;

@JsonAutoDetect
@Entity
public class Company {

private int id;
private double price;
Private String Company;
private date date;
private String size;
private byte visible;
}

2 in controller, returns a map
Public @ResponseBody map<string,list<company>> View () throws Exception

3) So SPIRNG MVC returns the following JSON characters:
{"Total": "Data": [{' Price ': 71.72, "Company": "3m Co", "visible": 1, "id": 1, "size": "Large", "date": 1188615600000},{" Price ": 29.01," Company ":" Aloca
Inc "," visible ": 0," id ": 2," size ":" Medium "," date ": 1185937200000},{" Price ": 83.81," Company ":" Altria Group
Inc "," visible ": 0," id ": 3," size ":" Large "," date ": 1186110000000
。。。。。。。。。。。。。。。。

4) You can see that the return time format is not very good

5 So, you can set this:
@JsonSerialize (Using=jsondateserializer.class)
Public Date getDate () {
return date;
}
Where Jsondateserializer is a newly-written class that inherits the Jsonserializer
Import java.io.IOException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;

Import Org.codehaus.jackson.JsonGenerator;
Import org.codehaus.jackson.JsonProcessingException;
Import Org.codehaus.jackson.map.JsonSerializer;
Import Org.codehaus.jackson.map.SerializerProvider;
Import org.springframework.stereotype.Component;
@Component
public class Jsondateserializer extends jsonserializer<date>{

private static final SimpleDateFormat DateFormat = new SimpleDateFormat ("mm-dd-yyyy");

@Override
public void serialize (date date, Jsongenerator Gen, Serializerprovider provider)
Throws IOException, Jsonprocessingexception {

String formatteddate = Dateformat.format (date);

Gen.writestring (formatteddate);
}

}

It's simple, format it, and it's going to output JSON.
{"Total": "Data": [{"Price": 71.72, "Company": "3m Co", "visible": 1, "id": 1, "size": "Large", "date": "09-01-2007"},{" Price ": 29.01," Company ":" Aloca
Inc "," visible ": 0," id ": 2," size ":" Medium "," date ":" 08-01-2007 "},{" price ": 83.81," Company ":" Altria Group
Inc "," visible ": 0," id ": 3," size ":" Large "," date ":" 08-03-2007 "},{" price ": 52.55," Company ":" American Express Company ", "Visible": 1, "id": 4, "size": "Extra
Large "," date ":" 01-04-2008 "},{" price ": 64.13," Company ":" American International Group
Inc., "visible": 1, "id": 5, "size": "Small", "date": "03-04-2008"},{"price": 31.61, "Company": "At&t Inc.", "visible" : 0, "id": 6, "size": "Extra
Large "," date ":" 02-01-2008 "},{" price ": 75.43," Company ":" Boeing Co. "," visible ": 1," id ": 7," size ":" Large "," date ":" 01-01-2008 "},{": 67.27, "Company": "Caterpillar
Inc., "visible": 1, "id": 8, "size": "Medium", "date": "12-03-2007"},{"price": 49.37, "Company": "The
Inc., "visible": 1, "id": 9, "size": "Large", "date": "11-24-2007"},{"price": 40.48, "Company": "E.i du Pont de Nemours and Company "," visible ": 0," id ": Ten," Size ":" Extra
Large "," date ":" 05-09-2007 "}]," Success ": true}

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.