0

我有一个名为的类Lookup,它有两个属性:

public class Lookup {

    private String surveyName;
    private String GUID;    

    public Lookup(String name, String guid){
        this.surveyName = name;
        this.GUID = guid;   
    }

}

在另一堂课中,我有一个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);

我最终得到的 XML 是:

<Lookups>
  <Lookup>
    <GUID>bar</GUID>
  </Lookup>
</Lookups>

如您所见,它只序列化了 fieldGUID而不是序列化了 field surveyName。为什么它忽略了那个领域?

4

2 回答 2

1

您确定没有在其他地方修改 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;   
    }
}

输出:

<Lookups>
  <Lookup>
    <surveyName>foo</surveyName>
    <GUID>bar</GUID>
  </Lookup>
</Lookups>
于 2012-04-26T21:18:03.250 回答
0

愚蠢的我,这个错误完全在我身上。该字段name接收到一个空字符串,因此 XStream 肯定忽略了它。

于 2012-04-26T21:13:53.283 回答