gpt4 book ai didi

android - 配置 gson 为 wcf web 服务序列化日历类型

转载 作者:太空狗 更新时间:2023-10-29 15:22:18 25 4
gpt4 key购买 nike

我有一个包含表示 CreatedOn 日期的日历类型的对象。我已经获得了显示毫秒的适配器,但我也不确定如何输出设备配置的时区信息。

WCF 期望日期采用以下格式:"CreatedOn":"\/Date(1305003600000-0500)\/"

这是我目前拥有的:

Gson gson = new GsonBuilder()
.registerTypeAdapter(GregorianCalendar.class, new JsonSerializer<GregorianCalendar>() {

public JsonElement serialize(GregorianCalendar date, Type type, JsonSerializationContext context) {
return new JsonPrimitive("/Date(" + date.getTimeInMillis() + ")/");
}

}).create();

最佳答案

最后做了以下事情:

Gson gson = new GsonBuilder()
.registerTypeAdapter(GregorianCalendar.class, new JsonSerializer<GregorianCalendar>() {
public JsonElement serialize(GregorianCalendar date, Type type, JsonSerializationContext context) {
return new JsonPrimitive("\\/Date(" + date.getTimeInMillis() + "-0000)\\/");
}
}).create();

当我将我的对象作为 json StringEntity 添加到 HttpPost 请求时,我修复了转义的反斜杠:

String json = gson.toJson(data).replace("\\\\/", "\\/");

我仍然对实现此目的的“更好”方法感兴趣,如果它存在的话......

关于android - 配置 gson 为 wcf web 服务序列化日历类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6417940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com