I have a class called Lookup
that has two properties:
我有一个名为Lookup的类,它有两个属性:
public class Lookup {
private String surveyName;
private String GUID;
public Lookup(String name, String guid){
this.surveyName = name;
this.GUID = guid;
}
}
In another class, I have a list of Lookup
that I am trying to serialize and save to file. This is how I'm doing it:
在另一个类中,我有一个Lookup列表,我正在尝试序列化并保存到文件。这就是我这样做的方式:
List<Lookup> lookup = new ArrayList<Lookup>();
lookup.add(new Lookup("foo","bar"));
XStream serializer = new XStream();
serializer.alias("Lookups",List.class);
String xml = serializer.toXML(lookup);
The XML I end up with is:
我最终得到的XML是:
<Lookups>
<Lookup>
<GUID>bar</GUID>
</Lookup>
</Lookups>
As you can see, it only serialized the field GUID
but not the field surveyName
. Why is it ignoring that field?
如您所见,它仅序列化了字段GUID,但没有序列化字段surveyName。为什么忽略那个领域?
2 个解决方案
#1
1
Are you sure that you don't modify Lookup variable somewhere else. This code runs fine
您确定不要在其他地方修改Lookup变量吗?这段代码运行正常
public class Test {
public static void main(String[] args) {
List<Lookup> lookup = new ArrayList<Lookup>();
lookup.add(new Lookup("foo","bar"));
XStream serializer = new XStream();
serializer.alias("Lookups",List.class);
String xml = serializer.toXML(lookup);
System.out.println(xml);
}
}
class Lookup {
private String surveyName;
private String GUID;
public Lookup(String name, String guid){
this.surveyName = name;
this.GUID = guid;
}
}
Output:
输出:
<Lookups>
<Lookup>
<surveyName>foo</surveyName>
<GUID>bar</GUID>
</Lookup>
</Lookups>
#2
0
Silly me, the mistake was completely on my end. The field name
was receiving an empty string, and thus XStream must have been ignoring it.
傻傻的,这个错误完全在我身上。字段名称正在接收一个空字符串,因此XStream必须忽略它。
#1
1
Are you sure that you don't modify Lookup variable somewhere else. This code runs fine
您确定不要在其他地方修改Lookup变量吗?这段代码运行正常
public class Test {
public static void main(String[] args) {
List<Lookup> lookup = new ArrayList<Lookup>();
lookup.add(new Lookup("foo","bar"));
XStream serializer = new XStream();
serializer.alias("Lookups",List.class);
String xml = serializer.toXML(lookup);
System.out.println(xml);
}
}
class Lookup {
private String surveyName;
private String GUID;
public Lookup(String name, String guid){
this.surveyName = name;
this.GUID = guid;
}
}
Output:
输出:
<Lookups>
<Lookup>
<surveyName>foo</surveyName>
<GUID>bar</GUID>
</Lookup>
</Lookups>
#2
0
Silly me, the mistake was completely on my end. The field name
was receiving an empty string, and thus XStream must have been ignoring it.
傻傻的,这个错误完全在我身上。字段名称正在接收一个空字符串,因此XStream必须忽略它。