json資料轉換異常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

來源:互聯網
上載者:User

標籤:desc   響應   商城   this   from   java   date()   set   string   

 1 //儲存Product對象的集合是從,Product是從mysql資料庫中查詢並添加的 2 ArrayList<Product> list = new ArrayList<Product>();
3 4 //設定響應對象編碼 5 response.setCharacterEncoding("utf-8"); 6 response.setContentType("text/html;charset=utf-8"); 7 8 //將list集合轉換成json格式 9 JSONArray json = JSONArray.fromObject(list);10 11 //將json寫出瀏覽器12 response.getWriter().print(json);


Product類

public class Product implements Serializable{private static final long serialVersionUID = -992811686857962266L;private String pid; //商品編號private String pname; //商品名稱private Double market_price; //市場價格private Double shop_price; //商城價格private String pimage; //商品圖片private Date pdate;  //上架日期private Integer is_hot = 0; //是否熱門商品, 1表示熱門, 0表示不熱門private String pdesc; //商品描述private Integer pflag; //表示是否下架, 1表示下架, 0表示不下架private Category category; //商品所屬的分類對象public Product() {}public Product(String pid, String pname, Double market_price, Double shop_price, String pimage, Date pdate,Integer is_hot, String pdesc, Integer pflag, Category category) {this.pid = pid;this.pname = pname;this.market_price = market_price;this.shop_price = shop_price;this.pimage = pimage;this.pdate = pdate;this.is_hot = is_hot;this.pdesc = pdesc;this.pflag = pflag;this.category = category;}public String getPid() {return pid;}public void setPid(String pid) {this.pid = pid;}public String getPname() {return pname;}public void setPname(String pname) {this.pname = pname;}public Double getMarket_price() {return market_price;}public void setMarket_price(Double market_price) {this.market_price = market_price;}public Double getShop_price() {return shop_price;}public void setShop_price(Double shop_price) {this.shop_price = shop_price;}public String getPimage() {return pimage;}public void setPimage(String pimage) {this.pimage = pimage;}public Date getPdate() {return pdate;}public void setPdate(Date pdate) {this.pdate = pdate;}public Integer getIs_hot() {return is_hot;}public void setIs_hot(Integer is_hot) {this.is_hot = is_hot;}public String getPdesc() {return pdesc;}public void setPdesc(String pdesc) {this.pdesc = pdesc;}public Integer getPflag() {return pflag;}public void setPflag(Integer pflag) {this.pflag = pflag;}public Category getCategory() {return category;}public void setCategory(Category category) {this.category = category;}} 

在將list轉換成JSON格式的時候,從資料庫裡讀出來的是java.sql.Date賦值給了java.util.Date,轉化成JSONArray時出錯

就會出現 et.sf.json.JSONException: java.lang.reflect.InvocationTargetException 異常

 

json資料轉換異常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.