ajax請求json資料異常:nested exception is net.sf.json.JSONException: java.lang.reflect.InvocationTargetException] with root cause

來源:互聯網
上載者:User

標籤:.text   nts   reflect   util   nconf   請求   .json   問題   roc   

ajax請求json資料異常:nested exception is net.sf.json.JSONException: java.lang.reflect.InvocationTargetException] with root cause1.異常原因:所請求的json資料中包含java.util.date資料類型,但是在後台並沒有將其格式轉換2.解決方案:添加工具類DateJsonValueProcessor 
import java.text.SimpleDateFormat;import net.sf.json.JsonConfig;import net.sf.json.processors.JsonValueProcessor;public class DateJsonValueProcessor implements JsonValueProcessor{    private String format;    public DateJsonValueProcessor(String format) {        this.format = format;    }    public Object processArrayValue(Object value, JsonConfig jsonConfig) {        return null;    }    public Object processObjectValue(String key, Object value,            JsonConfig jsonConfig) {        if (value == null) {            return "";        }        if (value instanceof java.sql.Timestamp) {            String str = new SimpleDateFormat(format)                    .format((java.sql.Timestamp) value);            return str;        }        if (value instanceof java.util.Date) {            String str = new SimpleDateFormat(format)                    .format((java.util.Date) value);            return str;        }        return value.toString();    }}
3.然後在你的controller中寫入
JsonConfig cfg = new JsonConfig();  cfg.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));  JSONArray json = JSONArray.fromObject(users, cfg);
    @RequestMapping("/getAllJsonUser")    public void getAllJsonUser(HttpServletResponse response){        List<User> users = userService.listAll();        System.out.println(users);        JsonConfig cfg = new JsonConfig();          cfg.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));          JSONArray json = JSONArray.fromObject(users, cfg);        JsonUtils.ajaxJson(json.toString(), response);    }

ok,問題解決

ajax請求json資料異常:nested exception is net.sf.json.JSONException: java.lang.reflect.InvocationTargetException] with root cause

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.