fastjson转换英文格式日期包数字转换异常处理
fastjson
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
项目地址:https://gitcode.com/gh_mirrors/fastj/fastjson
免费下载资源
·
说明:用到的fastjson版本是:fastjson-1.2.17
实体类:
package com.test;
import java.util.Date;
public class User {
private Integer id;
private String name;
private Date createDate;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", createDate=" + createDate + "]";
}
}
调用代码
public static void main(String[] args) throws ParseException {
String json = "[{\"id\":1000,\"name\":\"张三\",\"createDate\":\"Nov 24, 2016 2:54:26 PM\"},{\"id\":1000,\"name\":\"张三\",\"createDate\":\"Nov 24, 2016 2:54:26 PM\"}]";
List<User> users = JSONArray.parseArray(json, User.class);
System.out.println(users);
}
报错信息类似如下:
Exception in thread "main" java.lang.NumberFormatException: For input string: "Nov 24, 2016 2:54:26 PM"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at com.alibaba.fastjson.serializer.DateCodec.cast(DateCodec.java:202)
at
com.alibaba.fastjson.parser.deserializer.AbstractDateDeserializer.deserialze(AbstractDateDeserializer.java:150)
at com.alibaba.fastjson.parser.deserializer.DefaultFieldDeserializer.parseField(DefaultFieldDeserializer.java:66)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseField(JavaBeanDeserializer.java:722)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:568)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:187)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:183)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:708)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:644)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:639)
at com.alibaba.fastjson.JSON.parseArray(JSON.java:503)
at com.test.Main.main(Main.java:39)
解决方法如下(找到日期对应的源码解析类):
package com.alibaba.fastjson.parser.deserializer;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.JSONLexer;
import com.alibaba.fastjson.parser.JSONScanner;
import com.alibaba.fastjson.parser.JSONToken;
import com.alibaba.fastjson.util.TypeUtils;
public abstract class AbstractDateDeserializer extends ContextObjectDeserializer implements ObjectDeserializer {
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
return deserialze(parser, clazz, fieldName, null, 0);
}
@Override
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName, String format, int features) {
JSONLexer lexer = parser.lexer;
Object val;
if (lexer.token() == JSONToken.LITERAL_INT) {
val = lexer.longValue();
lexer.nextToken(JSONToken.COMMA);
} else if (lexer.token() == JSONToken.LITERAL_STRING) {
String strVal = lexer.stringVal();
if (format != null) {
SimpleDateFormat simpleDateFormat = null;
try {
simpleDateFormat = new SimpleDateFormat(format);
} catch (IllegalArgumentException ex) {
if (format.equals("yyyy-MM-ddTHH:mm:ss.SSS")) {
format = "yyyy-MM-dd'T'HH:mm:ss.SSS";
simpleDateFormat = new SimpleDateFormat(format);
} else if (format.equals("yyyy-MM-ddTHH:mm:ss")) {
format = "yyyy-MM-dd'T'HH:mm:ss";
simpleDateFormat = new SimpleDateFormat(format);
}
}
try {
val = simpleDateFormat.parse(strVal);
} catch (ParseException ex) {
if (format.equals("yyyy-MM-dd'T'HH:mm:ss.SSS") //
&& strVal.length() == 19) {
try {
val = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(strVal);
} catch (ParseException ex2) {
// skip
val = null;
}
} else {
**//====这里添加日期转换代码=====**
**//====这里添加日期转换代码=====**
**//====这里添加日期转换代码=====**
**//====这里添加日期转换代码=====**
try {
SimpleDateFormat ssddff = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss aa", Locale.ENGLISH);
val = ssddff.parse(strVal);
} catch (ParseException e) {
// skip
val = null;
}
}
}
} else {
val = null;
}
if (val == null) {
val = strVal;
lexer.nextToken(JSONToken.COMMA);
if (lexer.isEnabled(Feature.AllowISO8601DateFormat)) {
JSONScanner iso8601Lexer = new JSONScanner(strVal);
if (iso8601Lexer.scanISO8601DateIfMatch()) {
val = iso8601Lexer.getCalendar().getTime();
}
iso8601Lexer.close();
}
}
} else if (lexer.token() == JSONToken.NULL) {
lexer.nextToken();
val = null;
} else if (lexer.token() == JSONToken.LBRACE) {
lexer.nextToken();
String key;
if (lexer.token() == JSONToken.LITERAL_STRING) {
key = lexer.stringVal();
if (JSON.DEFAULT_TYPE_KEY.equals(key)) {
lexer.nextToken();
parser.accept(JSONToken.COLON);
String typeName = lexer.stringVal();
Class<?> type = TypeUtils.loadClass(typeName, parser.getConfig().getDefaultClassLoader());
if (type != null) {
clazz = type;
}
parser.accept(JSONToken.LITERAL_STRING);
parser.accept(JSONToken.COMMA);
}
lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
} else {
throw new JSONException("syntax error");
}
long timeMillis;
if (lexer.token() == JSONToken.LITERAL_INT) {
timeMillis = lexer.longValue();
lexer.nextToken();
} else {
throw new JSONException("syntax error : " + lexer.tokenName());
}
val = timeMillis;
parser.accept(JSONToken.RBRACE);
} else if (parser.getResolveStatus() == DefaultJSONParser.TypeNameRedirect) {
parser.setResolveStatus(DefaultJSONParser.NONE);
parser.accept(JSONToken.COMMA);
if (lexer.token() == JSONToken.LITERAL_STRING) {
if (!"val".equals(lexer.stringVal())) {
throw new JSONException("syntax error");
}
lexer.nextToken();
} else {
throw new JSONException("syntax error");
}
parser.accept(JSONToken.COLON);
val = parser.parse();
parser.accept(JSONToken.RBRACE);
} else {
val = parser.parse();
}
return (T) cast(parser, clazz, fieldName, val);
}
protected abstract <T> T cast(DefaultJSONParser parser, Type clazz, Object fieldName, Object value);
}
这样改还是有问题,还是会报数之前的错,还需要在实体类上加注解@JSONField(format=”yyyy-MM-dd”),修改后代码如下:
package com.test;
import java.util.Date;
import com.alibaba.fastjson.annotation.JSONField;
public class User {
private Integer id;
private String name;
@JSONField(format="yyyy-MM-dd")
private Date createDate;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", createDate=" + createDate + "]";
}
}
然后再运行代码就可以正常通过了
具体如何改源码,可以通过maven将源码下载下来,自己新建一个工程将源码导入到新建的工程中,直接修改即可。(报错无需理会,直接忽略即可)
修改后的代码打成一个jar,放到自己的项目中即可正常使用。
项目截图:
源码位置:
GitHub 加速计划 / fastj / fastjson
3
1
下载
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
最近提交(Master分支:4 个月前 )
c942c834 - 1 年前
5bc4709b - 1 年前
更多推荐
已为社区贡献2条内容
所有评论(0)