JSON Data format

Asked

Viewed 3,709 times

0

Hello personal I have a webservice that should fetch some infections in the database and return a json object and I’m having problems with date.

here is the object

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;


@XmlRootElement
public class Carro{

    @XmlElement
    private String id;

    @XmlElement
    private String nome;

    @XmlElement
    private Timestamp dtFabricacao;
... gets and setters

}

i go to the database and fill in the class properties normally the date is "dd/mm/yyyy HH:mm:ss z"

but json gets like this

{
"id": 1
"nome": "Fiesta"
"dtFabricacao": 152679516567919
}

I have tried to create a class of serialization and annotations and nothing works anymore I do not know what to do.

I am using Jax and Jackson to convert the object to json and my project uses Maven

1 answer

2

This is the date format in Json, if you are using javascript to consume this service you can use the date this way:

var date = new Date(152679516567919);
alert(date);

If you want to format otherwise you can use this good plugin:

$.format.date(new Date(), 'yyyy/MM/dd HH:mm:ss');

https://github.com/phstc/jquery-dateFormat

If you want to send in the format you see in the database, create another property but as string type this code and already fill with the formatted value:

@XmlElement
private String dtFabricacao;
  • Very good, perfect answer and completely useful

Browser other questions tagged

You are not signed in. Login or sign up in order to post.