Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Gson-Postgres时间戳序列化_Java_Android_Postgresql_Gson - Fatal编程技术网

Java Gson-Postgres时间戳序列化

Java Gson-Postgres时间戳序列化,java,android,postgresql,gson,Java,Android,Postgresql,Gson,问题在于使用GSON的Postegres时间戳序列化 private static final GsonBuilder GSON_BASE = Converters .registerAll(new GsonBuilder().disableHtmlEscaping()) .registerTypeAdapter(InfoTransfer.class, new InfoTransfer.Adapter()) .setDateFormat(DateF

问题在于使用GSON的Postegres时间戳序列化

private static final GsonBuilder GSON_BASE = Converters
        .registerAll(new GsonBuilder().disableHtmlEscaping())
        .registerTypeAdapter(InfoTransfer.class, new InfoTransfer.Adapter())
        .setDateFormat(DateFormat.FULL, DateFormat.FULL) //Line added but it seems work for MySQL DB Timestamp
        .setPrettyPrinting()
        .create();
.
//inner class in InfoTransfer 
public static class Adapter implements JsonSerializer<InfoTransfer>{

    @Override
    public JsonElement serialize(InfoTransfer src, Type typeOfSrc, JsonSerializationContext context) {

        Gson gson= new Gson();
        JsonObject jsonObject= (JsonObject) gson.toJsonTree(src);

        return new JsonPrimitive(jsonObject.getAsString());
    }
}
.
.
.
Log.d("Result",GSON_BASE.toJson(data));
结果:

\"created_at\": \"\\u0000\\u0001\\ufffdM\\u0015q\\ufffd}\"

有什么建议吗?

日期格式可以设置如下:-

setDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSSX")
示例:-

public static void main(String[] args) {

        String jsonString = "{\"customorId\":\"506\",\"joiningDate\":\"2016-10-26 19:49:17.290671+01\"}";

        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSSX") 
                .setPrettyPrinting()
                .create();


        //Deserialize
        Customer customer = gson.fromJson(jsonString, Customer.class);
        System.out.println(customer.toString());

        //Serialize
        Customer customerSerialize = new Customer();
        customerSerialize.setCustomorId("123");
        customerSerialize.setJoiningDate(new Date());

        System.out.println(gson.toJson(customerSerialize));


    }
public class Customer implements Serializable {
    private static final long serialVersionUID = 1100012615187080642L;

    private String customorId;

    private Date joiningDate;

}
Customer [customorId=506, joiningDate=Wed Oct 26 19:54:07 BST 2016]
{
  "customorId": "123",
  "joiningDate": "2016-10-26 20:23:37.000811+01"
}
客户类别:-

public static void main(String[] args) {

        String jsonString = "{\"customorId\":\"506\",\"joiningDate\":\"2016-10-26 19:49:17.290671+01\"}";

        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSSX") 
                .setPrettyPrinting()
                .create();


        //Deserialize
        Customer customer = gson.fromJson(jsonString, Customer.class);
        System.out.println(customer.toString());

        //Serialize
        Customer customerSerialize = new Customer();
        customerSerialize.setCustomorId("123");
        customerSerialize.setJoiningDate(new Date());

        System.out.println(gson.toJson(customerSerialize));


    }
public class Customer implements Serializable {
    private static final long serialVersionUID = 1100012615187080642L;

    private String customorId;

    private Date joiningDate;

}
Customer [customorId=506, joiningDate=Wed Oct 26 19:54:07 BST 2016]
{
  "customorId": "123",
  "joiningDate": "2016-10-26 20:23:37.000811+01"
}
样本输出:-

public static void main(String[] args) {

        String jsonString = "{\"customorId\":\"506\",\"joiningDate\":\"2016-10-26 19:49:17.290671+01\"}";

        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSSX") 
                .setPrettyPrinting()
                .create();


        //Deserialize
        Customer customer = gson.fromJson(jsonString, Customer.class);
        System.out.println(customer.toString());

        //Serialize
        Customer customerSerialize = new Customer();
        customerSerialize.setCustomorId("123");
        customerSerialize.setJoiningDate(new Date());

        System.out.println(gson.toJson(customerSerialize));


    }
public class Customer implements Serializable {
    private static final long serialVersionUID = 1100012615187080642L;

    private String customorId;

    private Date joiningDate;

}
Customer [customorId=506, joiningDate=Wed Oct 26 19:54:07 BST 2016]
{
  "customorId": "123",
  "joiningDate": "2016-10-26 20:23:37.000811+01"
}

谢谢兄弟:),如果您以字符串格式获取日期^^,这个解决方案就可以工作,但在我的例子中,我们希望序列化Postgres时间戳,因此我们使用更新web服务以字符串格式发送它。我将发布解决方案,它与yoursOk相同。该解决方案基于OP的预期结果。此外,我已经在postgres中使用NOW()对其进行了验证(NOW()是一个传统的PostgreSQL,相当于当前的_时间戳)