Json解析出错 :MorphDynaBean cannot be cast to

 在json字符串转java bean时,一般的对象,可以直接转,如:一个学生类,属性有姓名、年龄等;

但是如果属性中含有复杂的类型,当其中属性有类似List , Map ,ArrayList、自定义的类型,如List<Teacher> teachers, 就不可以了。 会报错:MorphDynaBean cannot be cast to con.test……

    在JSONObject.toBean的时候如果转换的类中有集合,可以先定义Map<String, Class> classMap = new HashMap<String, Class>();在classMap中put你要转换的类中的集合名,像:classMap.put("teachers", Teacher.class);然后在toBean()的时候把参数加上, 像:Student student=(Student) JSONObject.toBean(str, Student.class, classMap);

	private RSPerfData getRSPerfDataFromJSON(String jsonString) throws Exception{
		HashMap<String, Class> classMap = new HashMap<String, Class>(); 
		classMap.put("empDataData", RSEmpData.class);
        JSONObject jsonObject = JSONObject.fromObject(jsonString);
        WBQEncode_Decoder.my_decode_v2(jsonObject);
        RSPerfData rsPerfData = (RSPerfData) JSONObject.toBean(jsonObject, RSPerfData.class, classMap);
		return rsPerfData;
	}
public class WBQEncode_Decoder
{

    public static void my_decode_v2(Object object) throws Exception
    {
    	if (object instanceof JSONObject)
        {// 是對象
            JSONObject jsonObject = (JSONObject) object;
            handleJsonObject(jsonObject);
        } else if (object instanceof JSONArray)
        {// 是對象
            JSONArray jsonArray = (JSONArray) object;
            int length = jsonArray.size();
            for (int i = 0; i < length; i++)
            {
                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                handleJsonObject(jsonObject);
                jsonArray.set(i, jsonObject);
            }
        }
    }

    public static void my_encode_v2(List<Map<String, Object>> list) throws IOException
    {
        if (list == null || list.isEmpty())
        {
            return;
        }
        for (Iterator<Map<String, Object>> iterator = list.iterator(); iterator.hasNext();)
        {
            Map<String, Object> map = iterator.next();
            Set<String> keys = map.keySet();
            for (Iterator<String> iterator2 = keys.iterator(); iterator2.hasNext();)
            {
                String key = iterator2.next();
                Object object = map.get(key);
                if (object == null || object instanceof String || object instanceof Number)
                {
                    if (object == null)
                    {
                        object = "";
                    }
                    String value = object.toString();
                    if (value.equals("null") || value.equals("undefined"))
                    {
                        value = "";
                    }
                    value = URLEncoder.encode(value, "utf-8");
                    value = value.replaceAll("\\+", "百分之20(直接寫的話博客上看不到)");// 其實+號是URLEncoder.encode後把空格轉成+了,在頁面上解碼的時候是把空格當作了,所以要把+替換成

                    map.put(key, value);
                }
            }
        }
    }

    @SuppressWarnings("unchecked")
    private static void handleJsonObject(JSONObject jsonObject) throws Exception
    {
        Set<String> keys = jsonObject.keySet();
        for (Iterator<String> iterator = keys.iterator(); iterator.hasNext();)
        {
            String key = iterator.next();
            String objectStr = jsonObject.get(key).toString();
            if ((objectStr.startsWith("{") && objectStr.endsWith("}")))
            {
                my_decode_v2(JSONObject.fromObject(objectStr));
            } else if (objectStr.startsWith("[") && objectStr.endsWith("]"))
            {
                JSONArray jay = JSONArray.fromObject(objectStr);
                my_decode_v2(jay);
                jsonObject.put(key, jay);
            } else
            {
                objectStr = URLDecoder.decode(objectStr, "utf-8");
                if (objectStr.equals("null") || objectStr.equals("undefined"))
                {
                    objectStr = "";
                }
                
                objectStr = URLDecoder.decode(objectStr ,"utf-8");
                
                jsonObject.put(key, objectStr);
            }
        }
    }
}

[ERROR]java.lang.NoSuchMethodException .<init>()

有可能是你的转换的class非public。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值