Gson:JsonSyntaxException on date [英] Gson: JsonSyntaxException on date

查看:339
本文介绍了Gson:JsonSyntaxException on date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Gson反序列化一个json数组,但是我正在获得一个JsonSyntaxException。 json字符串由使用JsonResult的.NET MVC3 Web服务创建(意思是我不是手动创建json,它是由我知道在另外几个平台上工作的一个库创建的)。



这是json:

  [{PostID:1,StudentID:39 ,StudentName:Joe Blow,
Text:Test message。,CreateDate:\ / Date(1350178408267)\ /,
ModDate \ / Date(1350178408267)\ /,CommentCount:0}]

代码:

  public class Post {
public int PostID;
public int StudentID;
public String StudentName;
public String Text;
public Date CreateDate;
public Date ModDate;

public Post(){}
}

类型listOfPosts = new TypeToken< ArrayList< Post>>(){}。getType();
ArrayList< Post> posts = new Gson()。fromJson(json,listOfPosts);

异常说日期格式无效:

  com.google.gson.JsonSyntaxException:/ Date(1350178408267)/ 

任何人都知道发生了什么?

解决方案

我发现一个答案这里,但我发现奇怪的是没有更简单的方法。我使用的其他几个json库本身支持.NET json格式。当Gson没有处理它时,我很惊讶。必须有一个更好的方法。如果有人知道,请在这里发贴。所有这一切,这是我的解决方案:



我创建了一个自定义JsonDeserializer并将其注册为Date类型。通过这样做,Gson将使用我的解串器作为Date类型而不是默认值。如果你想以自定义的方式序列化/反序列化,那么对任何其他类型都可以这样做。

  public class JsonDateDeserializer implements JsonDeserializer< Date> {
public Date deserialize(JsonElement json,Type typeOfT,JsonDeserializationContext context)throws JsonParseException {
String s = json.getAsJsonPrimitive()。getAsString();
long l = Long.parseLong(s.substring(6,s.length() - 2));
日期d =新日期(l);
return d;
}
}

然后,当我创建我的Gson对象时: / p>

  Gson gson = new GsonBuilder()。registerTypeAdapter(Date.class,new JsonDateDeserializer())。 

现在我的gson对象将能够解析.NET日期格式(1970年以来的millis) / p>

I am trying to use Gson to deserialize a json array, but am currently getting a JsonSyntaxException. The json string was created by a .NET MVC3 web service using JsonResult (meaning, I am not manually creating the json, it is being created by a library which I know to work on several other platforms).

This is the json:

[{"PostID":1,"StudentID":39,"StudentName":"Joe Blow",
"Text":"Test message.","CreateDate":"\/Date(1350178408267)\/",
"ModDate":"\/Date(1350178408267)\/","CommentCount":0}]

This is the code:

public class Post {
   public int PostID;
   public int StudentID;
   public String StudentName;
   public String Text;
   public Date CreateDate;
   public Date ModDate;

   public Post() { }
}

Type listOfPosts = new TypeToken<ArrayList<Post>>(){}.getType();
ArrayList<Post> posts = new Gson().fromJson(json, listOfPosts);

The exception says that the date format is invalid:

com.google.gson.JsonSyntaxException: /Date(1350178408267)/

Anyone know what is going on?

解决方案

I found an answer here but I found it strange that there isn't an easier way. Several other json libraries I've used support the .NET json format natively. I was surprised when Gson didn't handle it. There must be a better way. If anyone knows of one, please post it here. All the same, this was my solution:

I created a custom JsonDeserializer and registered it for the Date type. By doing so, Gson will use my deserializer for the Date type instead of its default. The same can be done for any other type if you want to serialize/deserialize it in a custom way.

public class JsonDateDeserializer implements JsonDeserializer<Date> {
   public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
      String s = json.getAsJsonPrimitive().getAsString();
      long l = Long.parseLong(s.substring(6, s.length() - 2));
      Date d = new Date(l);
      return d; 
   } 
}

Then, when I am creating my Gson object:

Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDateDeserializer()).create();

Now my gson object will be capable of parsing the .NET date format (millis since 1970).

这篇关于Gson:JsonSyntaxException on date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆