Json data conversion exception: net. sf. json. JSONException: java. lang. reflect. InvocationTargetException

Source: Internet
Author: User
Run: JSONArrayarrayJSONArrayfromObject (thisusers); the following error will be reported: netsfjsonJSONException: javalangreflectInvocationTargetExceptionusers is a list set solution 1: JSONArrayarr

Run:
JSONArray array = JSONArray. fromObject (this. users );

The following error is reported:
Net. sf. json. JSONException: java. lang. reflect. InvocationTargetException

Users is a list set.


Solution 1:

JSONArray array = JSONArray. fromObject (this. users. toArray ());

Solution 2:

Because the bean contains the Date Field and the data read from the database is java. SQL. date is assigned to java. util. date, an error occurred when converting to JSONArray. You can directly write it as new java when reading the Date from the database. util. date (rs. getDate ("date "). getTime), so there will be no errors;

Solution 3:

Date Format hibernate delayed Loading

1. Solution: Date Format

Private java. util. Date createTime;

If the data type of Date is declared only before the field, an exception may be thrown. In the Set or get method, if the data type of Date is declared, the package name is added.

2. Solution: hibernate delayed loading settings

Java code
    JsonConfig cfg = new JsonConfig (); cfg. setExcludes (new String [] {"handler", "hibernateLazyInitializer "});

    Example

    Java code
      /*** Datagrid easyui finds out the contact pager-public, and you can view the self-created Contact */@ Transactional (readOnly = true) public JSONArray datagrid (Pager page, User user User, detachedCriteria detachedCriteria) {// cascade, cannot be converted directly. You need to extract the List and put it into the map. JsonConfig cfg = new JsonConfig (); // filter the Association to avoid endless loops. net. sf. json. JSONException: java. lang. reflect. invocationTargetException cfg. setJsonPropertyFilter (new PropertyFilter () {public boolean apply (Object source, String name, Object value) {if (name. equals ("addressGroup") | name. equals ("user") | name. equals ("createTime") | name. equals ("birthday") {return true;} else {return false ;}}); // net. sf. json. JSONException: java. lang. reflect. invocationTargetException exception cfg. setExcludes (new String [] {"handler", "hibernateLazyInitializer"}); // loop call appears in javabean. Use excludes to kill parent or children // cfg. setExcludes (new String [] {"addressGroup"}); // net. sf. json. JSONException: java. lang. reflect. invocationTargetException Date Format Conversion error // cfg. setCycleDetectionStrategy (CycleDetectionStrategy. LENIENT); // cfg. registerJsonValueProcessor (Date. class, new DateJsonValueProcessor ("yyyy-MM-dd hh: mm: ss"); Pager pager = this. findAllByApprove (page, user, detachedCriteria); long total = pager. getTotalCount (); List list = pager. getResult (); Map Result = new HashMap (); Result. put ("total", total); result. put ("rows", list); JSONArray jsonArray = JSONArray. fromObject (result, cfg); return jsonArray ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.