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

1.json工具类

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.实体类

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.测试类

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)

异常出现原因是 因为 实体类中没有空的构造方法,加上 空的构造方法,问题解决

posted @ 2017-03-02 12:24  小葱拌豆腐~  阅读(24898)  评论(1编辑  收藏  举报