主题:一个json-lib.jar转换复杂对象的问题?
相关的bean如下
TVO.java代码如下
Java代码
- package demo;
- import java.util.*;
- import com.easylotto.lottobar.bean.BaseEntity;
- import com.easylotto.lottobar.bean.ReqCommandVO;
- public class TVO extends BaseEntity{
- private String tname;
- private List<Person> rcvoList;
- private Map map;
- public List<Person> getRcvoList() {
- return rcvoList;
- }
- public void setRcvoList(List<Person> rcvoList) {
- this.rcvoList = rcvoList;
- }
- public String getTname() {
- return tname;
- }
- public void setTname(String tname) {
- this.tname = tname;
- }
- public Map getMap() {
- return map;
- }
- public void setMap(Map map) {
- this.map = map;
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- }
- }
package demo; import java.util.*; import com.easylotto.lottobar.bean.BaseEntity; import com.easylotto.lottobar.bean.ReqCommandVO; public class TVO extends BaseEntity{ private String tname; private List<Person> rcvoList; private Map map; public List<Person> getRcvoList() { return rcvoList; } public void setRcvoList(List<Person> rcvoList) { this.rcvoList = rcvoList; } public String getTname() { return tname; } public void setTname(String tname) { this.tname = tname; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub } }
Person.java如下
Java代码
- package demo;
- public class Person {
- private String name;
- private String age;
- public Person(String name,String age){
- this.name=name;
- this.age=age;
- }
- public String getAge() {
- return age;
- }
- public void setAge(String age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
package demo; public class Person { private String name; private String age; public Person(String name,String age){ this.name=name; this.age=age; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
测试类如下:
Java代码
- package demo;
- import net.sf.json.*;
- import java.util.*;
- import com.easylotto.lottobar.bean.ReqCommandVO;
- import com.easylotto.lottobar.bean.SocketCommandEntity;
- /**
- * Json使用测试类
- * @author peidw
- *
- */
- public class JsonDemo {
- public String JsonStr2Object(){
- String str="{'map':{'a2':'A2','a1':'A1'},'rcvoList':[{'age':'20','name':'小裴'},{'age':'58','name':'小庞'}],'tname':'xxname'}";
- JSONObject jo=JSONObject.fromObject(str);
- TVO sce=null;
- if(jo!=null){
- sce=(TVO)jo.toBean(jo, TVO.class);
- List alist=sce.getRcvoList();
- System.out.println(alist.size());
- Person rcvo=null;
- for(int i=0;i<alist.size();i++){
- rcvo=(Person)alist.get(i);
- System.out.print(rcvo.getName()+"-"+rcvo.getAge());
- }
- }
- return "测试";
- }
- public void Object2JsonStr(){
- TVO tvo=new TVO();
- tvo.setTname("xxname");
- List <Person>a=new ArrayList();
- a.add(new Person("小裴","20"));
- a.add(new Person("小庞","58"));
- tvo.setRcvoList(a);
- Map map=new HashMap();
- map.put("a1", "A1");
- map.put("a2", "A2");
- tvo.setMap(map);
- JSONObject jo=JSONObject.fromObject(tvo);
- System.out.println(jo);
- }
- public static void main(String[] args){
- JsonDemo jd=new JsonDemo();
- jd.JsonStr2Object();
- //jd.Object2JsonStr();
- }
- }
package demo; import net.sf.json.*; import java.util.*; import com.easylotto.lottobar.bean.ReqCommandVO; import com.easylotto.lottobar.bean.SocketCommandEntity; /** * Json使用测试类 * @author peidw * */ public class JsonDemo { public String JsonStr2Object(){ String str="{'map':{'a2':'A2','a1':'A1'},'rcvoList':[{'age':'20','name':'小裴'},{'age':'58','name':'小庞'}],'tname':'xxname'}"; JSONObject jo=JSONObject.fromObject(str); TVO sce=null; if(jo!=null){ sce=(TVO)jo.toBean(jo, TVO.class); List alist=sce.getRcvoList(); System.out.println(alist.size()); Person rcvo=null; for(int i=0;i<alist.size();i++){ rcvo=(Person)alist.get(i); System.out.print(rcvo.getName()+"-"+rcvo.getAge()); } } return "测试"; } public void Object2JsonStr(){ TVO tvo=new TVO(); tvo.setTname("xxname"); List <Person>a=new ArrayList(); a.add(new Person("小裴","20")); a.add(new Person("小庞","58")); tvo.setRcvoList(a); Map map=new HashMap(); map.put("a1", "A1"); map.put("a2", "A2"); tvo.setMap(map); JSONObject jo=JSONObject.fromObject(tvo); System.out.println(jo); } public static void main(String[] args){ JsonDemo jd=new JsonDemo(); jd.JsonStr2Object(); //jd.Object2JsonStr(); } }
出现的异常如下
2
Exception in thread "main" java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean
at demo.JsonDemo.JsonStr2Object(JsonDemo.java:26)
at demo.JsonDemo.main(JsonDemo.java:53)
使用的是json-lib-2.2.1-jdk15.jar包,请问这是怎么回事,怎么解决?
这个问题有两个地方值得考虑:
1、一个就是json-lib好像是使用反射实现的,那你的Person类如果没有无参数的构造方法的话,很危险;
2、在你的转化的代码上并没有告诉程序把rcvoList里的数据为何种类型
为此正确的做法是,为Person添加无参数的构造方法,同时修改代码为:
if(jo!=null){
Map<String, Class> m = new HashMap<String, Class>();
m.put("rcvoList", Person.class);
sce=(TVO)JSONObject.toBean(jo, TVO.class, m);
这样应该就OK了…………