public class ResData { private String retCode; private String retMessage; private ArrayList funds; public void setRetCode(String retCode){ this.retCode = retCode; } public String getRetCode(){ return retCode; } public void setRetMessage(String retMessage){ this.retMessage = retMessage; } public String getRetMessage(){ return retMessage; } public void setFunds(ArrayList funds){ this.funds = funds; } public ArrayList getFunds(){ return funds; } }
public class Fund { private String name; private String age; public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setAge(String age){ this.age = age; } public String getAge(){ return age; } }
public class MyTest { Fund fund1 = new Fund(); fund1.setName("zs"); fund1.setAge("20"); Fund fund2 = new Fund(); fund1.setName("ls"); fund1.setAge("22"); ArrayList funds = new ArrayList(); funds.add(fund1); funds.add(fund2); ResData data = new ResData(); data.setRetCode("007"); data.setRetMessage("Over"); data.setFunds(funds); //将bean序列化成json XStream xStream = new XStream(new JettisonMappedXmlDriver ()); xStream.alias("ResData",ResData.class); xStream.alias("funds",Fund.class); String json = jsonStream.toXML(data); System.out.println(json); //将json反序列化成bean(结果报错) XStream beanStream = new XStream(new JettisonMappedXmlDriver ()); beanStream.alias("ResData",ResData.class); beanStream.alias("funds",Fund.class); ResData conData = (ResData)beanStream.fromXML(json); }