XStream:使用XStreamImplicit忽略集合

时间:2012-07-05 07:59:25

标签: java json list xstream

我正在尝试使用XStream将小对象图输出到JSON。只输出,不需要反序列化。

对象非常简单:

@XStreamAlias("players")
public class Players {

    @XStreamImplicit
    private List<Player> players = new ArrayList<Player>();

    public Players() {
        for (int i = 0; i < 5; ++i) {
            players.add(new Player("Player " + i));
        }
    }
}

@XStreamAlias("player")
public class Player {
    private String name;
    public Player(String name) {
        this.name = name;
    }
}

幸运的是,JSON中省略了不必要的列表包装元素:

{ players: { player: { name: "Player 4"}}}

但不幸的是,只打印了最后一个元素

我正在使用XStream 1.4.2,像这样初始化:

XStream xstream = new XStream(new JsonHierarchicalStreamDriver());
xstream.setMode(XStream.NO_REFERENCES);
xstream.aliasSystemAttribute(null, "class");
xstream.autodetectAnnotations(true);
String out = xstream.toXML(xstramAliasObject);

据我所知,设置类似于这个问题:XStream Alias of List root elements

我的例子出了什么问题?

提前致谢! 此致,迈克尔

1 个答案:

答案 0 :(得分:0)

我正在使用相同的库,但却收到了整个消息..

<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>

<script>
  // 2. This code loads the IFrame Player API code asynchronously.
  var tag = document.createElement('script');

  tag.src = "https://www.youtube.com/iframe_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  // 3. This function creates an <iframe> (and YouTube player)
  //    after the API code downloads.
  var player;
  function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
      height: '390',
      width: '640',
      videoId: '0Bmhjf0rKe8',
      iv_load_policy: 3,
      showinfo:0,
      //videoId: 'Fj73JF_bhjc',
      events: {
        'onReady': onPlayerReady,
        'onStateChange': onPlayerStateChange
      }
    });
  }

  // 4. The API will call this function when the video player is ready.
  function onPlayerReady(event) {
    //event.target.playVideo();
  }

  // 5. The API calls this function when the player's state changes.
  //    The function indicates that when playing a video (state=1),
  //    the player should play for six seconds and then stop.
  var done = false;
  function onPlayerStateChange(event) {
      console.log('state changed');
      //player.seekTo(0, true);
    if (event.data == YT.PlayerState.PLAYING && !done) {
      done = true;
    }
  }
</script>

这是代码..

{"players": {
  "player": {
    "name": "Player 0"
  },
  "player": {
    "name": "Player 1"
  },
  "player": {
    "name": "Player 2"
  },
  "player": {
    "name": "Player 3"
  },
  "player": {
    "name": "Player 4"
  }
}}