最近做一个接口平台对接,soap协议,用jaxb转换了对方给的xsd文件为java类。
测试返回的结果对象中有很多带有XMLGregorianCalendar属性,转换成我自己系统需要的json串的时候是下面这个样子的:
"dateActivated": { "XMLSchemaType": { "localPart": "dateTime", "namespaceURI": "http://www.w3.org/2001/XMLSchema", "prefix": "" }, "day": 28, "eon": 0, "eonAndYear": 2016, "fractionalSecond": 0.284, "hour": 1, "millisecond": 284, "minute": 39, "month": 1, "second": 48, "timezone": 0, "valid": true, "year": 2016 },
需修改为正常时间格式,代码如下
JsonConfig jsonConfig =new JsonConfig() ; /** * format classType XMLGregorianCalendar */ jsonConfig.registerJsonValueProcessor(XMLGregorianCalendar.class, new JsonValueProcessor() { @Override public Object processObjectValue(String key, Object value, JsonConfig arg2) { // TODO Auto-generated method stub return new SimpleDateFormat(Constants.DATE_TIME_FORMAT).format(((XMLGregorianCalendar)value).toGregorianCalendar().getTime() );} @Override public Object processArrayValue(Object value, JsonConfig arg1) { // TODO Auto-generated method stub return value; }});System.out.println(JSONObject.fromObject(obj, jsonConfig).toString());
输出的json变成
"dateActivated":"2016-01-28 09:39:48",