![](/img/trans.png)
[英]Javascript & Json: Excluding Fields From Serialization and Deserialization
[英]Excluding some Object fields from serialization
我正在使用javax.xml.bind.annotation.XmlRootElement注释对象将其序列化为xml字符串。
JAXBContext jc = JAXBContext.newInstance(obj.getClass());
// Marshal the object to a StringWriter
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.example.com/schema.xsd");
StringWriter stringWriter = new StringWriter();
marshaller.marshal(obj, stringWriter);
result = stringWriter.toString();
如何排除对象的某些字段以便不发送它们? 为了将其从最终字符串中排除,必须对类字段的注释进行注释。
您可以使用transient
关键字来忽略要序列化的字段。
例如:
int k;
transient int j;
Variable j
将不会被序列化,因为我们已经提到了transient
关键字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.