解析ASP.NET MVC在Java中使用JSON杰克逊库返回日期 [英] Parsing ASP.NET MVC returned date using Jackson JSON library in Java

查看:203
本文介绍了解析ASP.NET MVC在Java中使用JSON杰克逊库返回日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用杰克逊JSON解析库从服务器JSON在我的Andr​​oid应用程序。然而,解析请求失败,每当我收到的日期时间,因为它的格式为:

I am parsing JSON from server in my Android application by using Jackson JSON library. However, parsing requests fail whenever I receive DateTime since it's in this format:

"/Date(1277931782420)/"

我知道我应该这样做:

I know I should do something like:

ObjectMapper om = new ObjectMapper();
om.setDateFormat(new TicksSinceFormat());

但我不知道我是否可以使用的SimpleDateFormat在所有(什么格式字符串我会用?),或者我需要写我自己的日期格式分析器。所以,我会认真AP preciate如果有人能帮助code的例子。

But I have no idea if I can use SimpleDateFormat at all (and what format string would I use?) or I need to write my own DateFormat parser. So, I would seriously appreciate if somebody could help with code example.

编辑:
OK,看我为完整的code答案。

OK, see my answer for complete code.

推荐答案

这被证明是更严厉的话,我预计:

This proved to be tougher then I expected:

public class TicksSinceFormat extends DateFormat {
    @Override
    public StringBuffer format(Date date, StringBuffer buffer, FieldPosition field) {
        long millis = date.getTime();

        return new StringBuffer("/Date(" + millis + ")/");
    }

    @Override
    public Date parse(String string, ParsePosition position) {
        int start = string.indexOf("(") + 1;
        int end = string.indexOf(")");

        String ms = string.substring(start, end);
        Date date = new Date(Long.parseLong(ms));

        position.setIndex(string.length() - 1); // MUST SET THIS
        return date;
    }

    @Override
    public Object clone() {
        return new TicksSinceFormat(); // MUST SET THIS
    }
}

使用类则是非常简单的,只是做:

Using class is then extremely simple, just do:

ObjectMapper om = new ObjectMapper();
om.setDateFormat(new TicksSinceFormat())

我presume,这可能是codeD +更好,我将需要处理的差异,当谈到.NET VS蜱蜱Java的 - 但现在这个会做。如果有人有更好的解决办法或更深入地了解提到的问题我会处理以后 - 随意张贴,我会标记您的答案正确,如果是更好的。

I presume that this can be coded better + that I'll need to deal with differences when it comes to .NET Ticks VS Java ticks - but for now this'll do. If somebody has better solution or more insight into mentioned problems I'll deal with later - feel free to post and I'll mark your answer as correct one if it's better.

编辑:正如我在这个已经解释<一个href=\"http://stackoverflow.com/questions/14973286/asp-net-mvc-json-datetime-serialization-conversion-to-utc\">question &安培;回答我切换到ServiceStack.Text库在服务器上,它返回不同,ISO8601的格式。对于格式我使用略有不同的解析(因为杰克逊有故障解析ISO8601包含毫秒)。当然,与其他code我张贴 - 让我知道,如果你有更好的版本(只是请邮寄code /编辑这个职位,而不是诉诸哲学修辞上应该怎么做):

As I've explained in this question & answer I've switched to ServiceStack.Text library on the server and it returns different, ISO8601 format. For that format I'm using slightly different parsing (since Jackson has trouble parsing ISO8601 that contains milliseconds). Of course, as with other code I'm posting - let me know if you have better version (just please post code / edit this post, rather than resorting to philosophical rhetoric on how it should be done):

@SuppressLint("SimpleDateFormat")
public class JacksonSimpleDateFormat extends SimpleDateFormat {
    public JacksonSimpleDateFormat() {
        if (mParser == null) {
            mParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            mParser.setTimeZone(TimeZone.getTimeZone("UTC"));
        }
    }

    @Override
    public StringBuffer format(Date date, StringBuffer buffer, FieldPosition field) {
        return mParser.format(date, buffer, field);
    }

    private static SimpleDateFormat mParser;
    @Override
    public Date parse(String string, ParsePosition position) {
        String str = string.split("\\.")[0];

        Date date = null;
        try {
            date = mParser.parse(str);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        position.setIndex(string.length() - 1);
        return date;
    }

    @Override
    public Object clone() {
        return new JacksonSimpleDateFormat();
    }
}

这篇关于解析ASP.NET MVC在Java中使用JSON杰克逊库返回日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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