开发者

Xstream giving different results

开发者 https://www.devze.com 2022-12-30 21:29 出处:网络
We are using XStream for our restful services. However, Xstream gives us varying results for fields with the same values.

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": ["开发者_如何学PythonmySubject"]
  }
}

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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号