我正在使用FlexJSON,在解析int中的Date对象时遇到了问题。我尝试这样使用JSONDeserializer:
String json = decryptJson(new String(personalInformationData));
return new JSONDeserializer<PersonalInformation>().deserialize(json);json的值是:
{"address1":"123 Fake St","address2":"#4","address3":"","city":"Springfield","class":"PersonalInformation","confirmEmailAddress":"foo@bar.com","coverageGroupName":"","coverageGroupNumber":"","coverageType":"I","dob":21600000,"emailAddress":"foo@bar.com","firstName":"Zapp","formOfId":"D","group":false,"idNum":"K201132083220","idState":"AL","individual":true,"lastName":"Brannigan","middleInitial":"","nonUsAddress":false,"nonUsAddress1":null,"nonUsAddress2":null,"nonUsAddress3":null,"phone":"(555) 555-5555","ssn":"555555555","state":"OH","zip":"55555"}除非出生日期(dob键)值介于1969年12月7日和1970年1月25日之间(或-2138400000到2095200000毫秒),否则FlexJSON会抛出此错误:
[JSONException: [ dob ]: Parsing date 21600000 was not recognized as a date format]我不确定这是怎么发生的,因为new Date(21600000)的计算结果是Thu Jan 01 00:00:00 CST 1970。
有没有人遇到过这种情况?
更新#1
因此,这个错误的发生似乎是因为JSONDeserializer不能处理保存为Unix TimeStamp的日期,这些日期的范围是1969年12月7日到1970年1月25日。超出该范围的任何其他日期都被接受,并且也是Unix TimeStamp。
我不认为我需要用.use()实现一个定制的ObjectFactory或者创建一个定制的转换器,因为其他的Unix TimeStamps不在失败的日期范围内。
更新#2
我尝试在序列化时实现transformer,使用以下命令将日期从Unix TimeStamp更改为日期格式的字符串:
String json = new JSONSerializer().transform(new DateTransformer("yyyy-caMM-dd"), "dob").serialize(personalInformation);这完全是按照它应该的方式工作的,但不是在反序列化时。我仍然收到相同的错误:
[JSONException: [ dob ]: Parsing date 1970-01-01 was not recognized as a date format]发布于 2012-12-13 03:47:22
这肯定是Flexjson的一个问题。我们仍然不能弄清楚这个问题,但我的同事设法想出了一个变通的办法,直到它被解决。本质上,我们创建一个新的DateTransformer并指定要使用的格式。然后,我们使用该转换器在序列化时转换Date.class,并在反序列化时通过use()再次使用该转换器。
The DateTransformer
private static final DateTransformer DATE_TRANSFORMER = new DateTransformer("MM/dd/yyyy");序列化:
String json = new JSONSerializer().transform(DATE_TRANSFORMER, Date.class).serialize(personalInformation);反序列化:
return new JSONDeserializer<PersonalInformation>().use(Date.class, DATE_TRANSFORMER).deserialize(json);发布于 2013-01-25 23:10:04
我也有同样的问题。通过扩展flexjson.factories.DateObjectFactory和覆盖instantiate()方法修复,就像这样。
@Override
public Object instantiate(ObjectBinder context, Object value, Type targetType, Class targetClass) {
if (value instanceof Integer) {
return super.instantiate(context, ((Integer) value).longValue(), targetType, targetClass);
}
return super.instantiate(context, value, targetType, targetClass);
}在那之后,只需做一些小把戏
JSONDeserializer<T> jsonDeserializer = new JSONDeserializer<T>().use(Date.class, new >YourExtendedDateObjectFactoryClass<)然后,您可以轻松地反序列化json字符串。
https://stackoverflow.com/questions/13810413
复制相似问题