关于json-lib将mysql中的date(time)转换成json对象出现java.sql.time not gethours问题

由于json-lib中将数据库中的date转化出现的这个问题。


public void setJsonResult1(Object obj) throws Exception{
PrintWriter out=null;
HttpServletResponse response=(HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
response.setContentType("text/plain;charset=utf-8");
out=response.getWriter();
JsonConfig jsonConfig=new JsonConfig();
jsonConfig.registerJsonValueProcessor(java.sql.Date.class, new JsonDateValueProcessor());
out.println(JSONArray.fromObject(obj, jsonConfig));
out.close();
}

创建一个类,将date数据转化为yyyy-MM-dd形式

import java.text.SimpleDateFormat;


import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;


public class JsonDateValueProcessor implements JsonValueProcessor {


private String format="yyyy-MM-dd";
@Override
public Object processArrayValue(Object arg0, JsonConfig arg1) {
// TODO Auto-generated method stub
return process(arg0);
}


private Object process(Object arg0) {
// TODO Auto-generated method stub
SimpleDateFormat sdf=new SimpleDateFormat(format);
return sdf.format(arg0);
}


@Override
public Object processObjectValue(String arg0, Object arg1, JsonConfig arg2) {
// TODO Auto-generated method stub
return process(arg1);
}


}

猜你喜欢

转载自blog.csdn.net/IamstudyingJava/article/details/46873707