Json解析出错 :MorphDynaBean cannot be cast to
在json字符串转java bean时,一般的对象,可以直接转,如:一个学生类,属性有姓名、年龄等;
但是如果属性中含有复杂的类型,当其中属性有类似List , Map ,ArrayList、自定义的类型,如List
在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 .
有可能是你的转换的class非public。
还没有评论,来说两句吧...