Xstream 给出不同的结果

发布于 2024-09-02 11:52:44 字数 616 浏览 5 评论 0原文

我们使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七七 2024-09-09 11:52:44

首先,上面的 Book 实例包含错误。我认为它应该如下所示:

public class Book{
    public String name = "myName";
    public List authors = new ArrayList();
    public String subject = "mySubject";

    public Book(){
    }
}

现在:

{"Book":{"name":"myName", "authors":"", "subject":["mySubject"]}}

您确定这就是 xstream 为上面列出的 Book 对象返回的内容吗?这似乎不对,因为 subject 属性是一个 String 而不是 String[] 或其他类型的集合。您给出的第一个示例(没有作者的书籍)的 JSON 编码应该是:

{"Book":{"name":"myName", "authors":"", "subject":"mySubject"}}

除非您的 Book 看起来像这样:

public class Book{
    public String name = "myName";
    public List authors = new List();
    public String[] subject = {"mySubject"};

    public Book(){
    }
}

底线:确保您没有将主题声明为集合。

作为额外提示,请尝试针对您的问题发布工作代码。这样更容易获得有意义的答案。
所以我的猜测是你的 Book 类声明 subject 是某种集合

In the first place, your Book instance above contains errors. Here's what I suppose it should look like:

public class Book{
    public String name = "myName";
    public List authors = new ArrayList();
    public String subject = "mySubject";

    public Book(){
    }
}

Now:

{"Book":{"name":"myName", "authors":"", "subject":["mySubject"]}}

Are you sure this is what xstream is returning for the Book object listed above? This doesn't seems right, since the subject property is a String and not a String[] or other type of collection. The JSON encoding for the first example you give (book without authors) should be:

{"Book":{"name":"myName", "authors":"", "subject":"mySubject"}}

Unless your Book looked something like this:

public class Book{
    public String name = "myName";
    public List authors = new List();
    public String[] subject = {"mySubject"};

    public Book(){
    }
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文