[json-lib-user] JSONObject.toBean() problem
Brought to you by:
aalmiray
From: <ki...@ro...> - 2006-11-23 11:24:03
|
Hi all. I've run the code in the 'Getting Started' document. ------------------------------------------------------------ public static class MyBean{ private List data; public List getData() { return data; } public void setData(List data) { this.data = data; } } public static class Person{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } public static void testTest() throws Exception { String json = "{'data':[{'name':'Wallace'},{'name':'Grommit'}]}"; JSONObject obj = new JSONObject(json); MyBean bean = (MyBean)JSONObject.toBean( obj, MyBean.class ); } ------------------------------------------------------------ And following exception occured. ------------------------------------------------------------ Exception in thread "main" net.sf.json.JSONException: java.lang.NoSuchMethodException: Unknown property 'name' at net.sf.json.JSONObject.toBean(JSONObject.java:439) at net.sf.json.JSONArray.toList(JSONArray.java:349) at net.sf.json.JSONObject.toBean(JSONObject.java:376) at net.sf.json.JSONObject.toBean(JSONObject.java:325) at com.rolizen.test.jsonlib.JsonLibTest.testTest(JsonLibTest.java:96) at com.rolizen.test.jsonlib.JsonLibTest.main(JsonLibTest.java:21) Caused by: java.lang.NoSuchMethodException: Unknown property 'name' at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:1741) at org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1648) at org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:1677) at org.apache.commons.beanutils.PropertyUtils.setProperty(PropertyUtils.java:559) at net.sf.json.JSONObject.setProperty(JSONObject.java:484) at net.sf.json.JSONObject.toBean(JSONObject.java:408) ... 5 more ------------------------------------------------------------ I think that at the 376 line in the JSONObject.java, invoking JSONArray.toList(...) method, the second parameter, 'beanClass' should not be passed. Instead, null should be passed, I think. The intended class of list item is 'Person', but 'beanClass' is 'MyBean', so PropertyUtils cannot find 'name' property from 'beanClass'. Doesn't anyone have this problem? Sorry for poor english. :( |