com.alibaba.fastjson.JSONException: default constructor not found. class ……

1.json工具類java

package com.hyzn.fw.util;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;

/** 
 * @ClassName: JsonUtil
 * @Description: TODO
 * @author xbq
 * @version 1.0
 * @date 2017-2-13 上午11:45:25
 */
public class JsonUtil {

    /**
     * @Title: jsonToList
     * @Description: TODO  json 轉爲 List
     * @param json
     * @param obj
     * @return
     * @return: List
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static List jsonToList(String json, Class clz){
        return JSON.parseArray(json, clz);
    }
    
    /**
     * @Title: jsonToMap
     * @Description: TODO json轉Map
     * @param json
     * @param clz
     * @return
     * @return: Map
     */
    @SuppressWarnings("rawtypes")
    public static Map jsonToMap(String json) {
        return JSON.parseObject(json,new TypeReference<Map<String,List>>(){});
    }
    
    
    /**
     * @Title: jsonToObject
     * @Description: TODO json轉實體
     * @param json
     * @param obj
     * @return
     * @return: Object
     */
    public static Object jsonToObject(String json,Object obj){
        return JSON.parseObject(json, obj.getClass());
    }
    
    /**
     * @Title: dealResponseJson
     * @Description: TODO  實體 轉爲 Json
     * @param obj
     * @return
     * @throws Exception
     * @return: String
     * 
     * QuoteFieldNames———-輸出key時是否使用雙引號,默認爲true 
     * WriteMapNullValue——–是否輸出值爲null的字段,默認爲false
     * WriteNullNumberAsZero—-數值字段若是爲null,輸出爲0,而非null 
     * WriteNullListAsEmpty—–List字段若是爲null,輸出爲[],而非null
     * WriteNullStringAsEmpty—字符類型字段若是爲null,輸出爲」「,而非null
     * WriteNullBooleanAsFalse–Boolean字段若是爲null,輸出爲false,而非null
     */
    public static String ObjectToJson(Object obj) throws Exception {
        return JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteDateUseDateFormat,SerializerFeature.WriteNullNumberAsZero); 
    }
}

2.實體類json

package com.hyzn.fw.controller;
/** 
 * @ClassName: Student
 * @Description: TODO
 * @author xbq
 * @version 1.0
 * @date 2017-3-2 上午11:42:20
 */
public class Student {

    private int ID;
    private String NAME;
    private String ADDRESS;
    
    public Student(int iD, String nAME, String aDDRESS) {
        super();
        ID = iD;
        NAME = nAME;
        ADDRESS = aDDRESS;
    }
    
    public int getID() {
        return ID;
    }
    public void setID(int iD) {
        ID = iD;
    }
    public String getNAME() {
        return NAME;
    }
    public void setNAME(String nAME) {
        NAME = nAME;
    }
    public String getADDRESS() {
        return ADDRESS;
    }
    public void setADDRESS(String aDDRESS) {
        ADDRESS = aDDRESS;
    }
    
    @Override
    public String toString() {
        return "Student [ID=" + ID + ", NAME=" + NAME + ", ADDRESS=" + ADDRESS
                + "]";
    }
}

3.測試類ide

package com.hyzn.fw.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import com.alibaba.fastjson.JSONObject;
import com.hyzn.fw.util.JsonUtil;

/** 
 * @ClassName: Test
 * @Description: TODO
 * @author xbq
 * @version 1.0
 * @date 2017-3-2 上午11:08:14
 */
public class Test {

    public static void main(String[] args) {
        
        List<Student> list = new ArrayList<Student>();
        Student s = new Student(1,"張三","廣州");
        list.add(s);
        s = new Student(2,"李四","廣州");
        list.add(s);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("TB_ROOM", JsonUtil.ObjectToJson(list));
            jsonObject.put("TB_FLOOR", JsonUtil.ObjectToJson(list));
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        String json = jsonObject.toJSONString();
        System.out.println(json);
        
        JSONObject jsonobj = JSONObject.parseObject(json);
        for(Entry<String, Object> jsonData : jsonobj.entrySet()){
            System.out.println("jsonData.getValue() ==" + jsonData.getValue());
            System.out.println("jsonData.getKey() ==" + jsonData.getKey());
            List<Student> lists = JsonUtil.jsonToList(jsonData.getValue().toString(), Student.class);
            System.out.println(lists);
        }
    }
}

運行測試類的時候 ,出現 異常,以下:工具

Exception in thread "main" com.alibaba.fastjson.JSONException: default constructor not found. class com.hyzn.fw.controller.Student
    at com.alibaba.fastjson.util.DeserializeBeanInfo.computeSetters(DeserializeBeanInfo.java:150)
    at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.<init>(JavaBeanDeserializer.java:43)
    at com.alibaba.fastjson.parser.ParserConfig.createJavaBeanDeserializer(ParserConfig.java:332)
    at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(ParserConfig.java:323)
    at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(ParserConfig.java:256)
    at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:542)
    at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:521)
    at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:516)
    at com.alibaba.fastjson.JSON.parseArray(JSON.java:336)
    at com.hyzn.fw.util.JsonUtil.jsonToList(JsonUtil.java:29)
    at com.hyzn.fw.controller.Test.main(Test.java:39)

異常出現緣由是 由於 實體類中沒有空的構造方法,加上 空的構造方法,問題解決測試

相關文章
相關標籤/搜索