Xstream 给出不同的结果
我们使用 XStream 来提供宁静的服务。然而,对于具有相同值的字段,Xstream 为我们提供了不同的结果。 假设它是 Book 对象:
public class Book {
public String name "myName";
public List <String> authors = new List <String> ();
public String subject "mySubject";
public Book() {}
}
json 是:
{
"Book": {
"name": "myName",
"authors": "",
"subject": ["mySubject"]
}
}
但是,如果我将作者添加到集合中,我会得到不同的结果。
{
"Book": {
"name": "myName",
"authors": ["author1", "author2", "author3"],
"subject": "mySubject"
}
}
有谁遇到过这个问题并知道解决方案吗?
We are using XStream for our restful services. However, Xstream gives us varying results for fields with the same values.
assume it Book object:
public class Book {
public String name "myName";
public List <String> authors = new List <String> ();
public String subject "mySubject";
public Book() {}
}
The json for this is:
{
"Book": {
"name": "myName",
"authors": "",
"subject": ["mySubject"]
}
}
However, if I add authors to the collection I get a different result.
{
"Book": {
"name": "myName",
"authors": ["author1", "author2", "author3"],
"subject": "mySubject"
}
}
Has anyone run into this issue and know of a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,上面的 Book 实例包含错误。我认为它应该如下所示:
现在:
您确定这就是 xstream 为上面列出的
Book
对象返回的内容吗?这似乎不对,因为subject
属性是一个String
而不是String[]
或其他类型的集合。您给出的第一个示例(没有作者的书籍)的 JSON 编码应该是:除非您的
Book
看起来像这样:底线:确保您没有将主题声明为集合。
作为额外提示,请尝试针对您的问题发布工作代码。这样更容易获得有意义的答案。
所以我的猜测是你的 Book 类声明 subject 是某种集合
In the first place, your Book instance above contains errors. Here's what I suppose it should look like:
Now:
Are you sure this is what xstream is returning for the
Book
object listed above? This doesn't seems right, since thesubject
property is aString
and not aString[]
or other type of collection. The JSON encoding for the first example you give (book without authors) should be:Unless your
Book
looked something like this:Bottom line: make sure you are not declaring your subject as a collection.
As a bonus tip, try to post working code on your questions. It's easier that way to get meaningful answers.
So my guess is that your
Book
class is declaring subject to be some kind of collection