– Start
点击此处观看本系列配套视频。
使用 XStream 不仅可以把对象转成 xml,也可以把对象转成JSON,下面是一个简单的例子。
package shangbo.xstream.example7;
import java.util.ArrayList;
import java.util.List;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
public class App {
public static void main(String[] args) {
// 实例化 XStream 对象
XStream xstream = new XStream(new JettisonMappedXmlDriver()); // 注意此处
xstream.alias("person", Person.class); // 自定义标签
xstream.alias("phone", PhoneNumber.class); // 自定义标签
xstream.aliasField("countrycode", PhoneNumber.class, "code"); // 自定义标签
xstream.addImplicitCollection(Person.class, "phones"); // 去掉集合标签
xstream.useAttributeFor(Person.class, "firstname"); // 生成属性,而非子标签
xstream.useAttributeFor(Person.class, "lastname"); // 生成属性,而非子标签
xstream.registerConverter(new PhoneNumberConverter()); // 注册转换器
// 将对象转成 json
String json = xstream.toXML(newPerson());
System.out.println(json);
// 将 json 转成对象
Person shangbo = (Person) xstream.fromXML(json);
System.out.println(shangbo);
}
private static Person newPerson() {
List<PhoneNumber> phones = new ArrayList<>();
PhoneNumber phone = new PhoneNumber();
phone.setCode("86");
phone.setNumber("0411-12345678");
phones.add(phone);
PhoneNumber cell = new PhoneNumber();
cell.setCode("86");
cell.setNumber("18912345678");
phones.add(cell);
Address address = new Address();
address.setState("LiaoNing");
address.setCity("DaLian");
address.setStreat("GaoXingQu");
Person shangbo = new Person();
shangbo.setFirstname("Bo");
shangbo.setLastname("Shang");
shangbo.setAddress(address);
shangbo.setPhones(phones);
return shangbo;
}
}
如下结果如下。
{"person":{"@firstname":"Bo","@lastname":"Shang","address":{"state":"LiaoNing","city":"DaLian","streat":"GaoXingQu"},"phone":["86 0411-12345678","86 18912345678"]}}
Security framework of XStream not initialized, XStream is probably vulnerable.
shangbo.xstream.example7.Person@1786f9d5[firstname=Bo,lastname=Shang,address=shangbo.xstream.example7.Address@2eda0940[state=LiaoNing,city=DaLian,streat=GaoXingQu],phones=[shangbo.xstream.example7.PhoneNumber@3578436e[code=86,number=0411-12345678], shangbo.xstream.example7.PhoneNumber@706a04ae[code=86,number=18912345678]]]
– 更多参见:XStream 精萃
– 声 明:转载请注明出处
– Last Updated on 2018-05-27
– Written by ShangBo on 2018-05-27
– End