我试图将Json结果从Web服务反序列化为POJO。如何使用Restlet将Json结果反序列化为POJO
ClientResource clientResource = new ClientResource("http://itunes.apple.com/search?term=marc+jordan&media=music&entity=album");
AlbumInfoResource resource = clientResource.wrap(AlbumInfoResource.class);
AlbumInfo albumInfo = resource.retrieve();
由此产生的albumInfo
为空,不会引发异常。
我是新来的Restlet,我做错了什么?
接口:
public interface AlbumInfoResource {
@Get
public AlbumInfo retrieve();
}
从Web服务JSON结果如下:
{
"resultCount": 49,
"results": [
{
"wrapperType": "collection",
"collectionType": "Album",
"artistId": 771969,
"collectionId": 205639995,
"amgArtistId": 4640,
"artistName": "Marc Jordan",
"collectionName": "This Is How Men Cry",
"collectionCensoredName": "This Is How Men Cry",
"artistViewUrl": "http://itunes.apple.com/us/artist/marc-jordan/id771969?uo=4",
"collectionViewUrl": "http://itunes.apple.com/us/album/this-is-how-men-cry/id205639995?uo=4",
"artworkUrl60": "http://a5.mzstatic.com/us/r30/Music/cd/3f/13/mzi.rxpvpvdd.60x60-50.jpg",
"artworkUrl100": "http://a1.mzstatic.com/us/r30/Music/cd/3f/13/mzi.rxpvpvdd.100x100-75.jpg",
"collectionPrice": 9.9,
"collectionExplicitness": "notExplicit",
"trackCount": 10,
"copyright": "1999 Cafe Productions Inc.",
"country": "USA",
"currency": "USD",
"releaseDate": "2006-11-07T08:00:00Z",
"primaryGenreName": "Jazz"
},
...
...
}
]
}
的AlbumInfo类:
public class AlbumInfo implements Serializable {
private static final long serialVersionUID = 1L;
private int _resultCount;
private ArrayList<Album> _albums;
public AlbumInfo() {
_albums = new ArrayList<Album>();
}
public AlbumInfo(int resultCount, ArrayList<Album> albums) {
_resultCount = resultCount;
_albums = albums;
}
public int getResultCount() {
return _resultCount;
}
public void setResultCount(int resultCount) {
_resultCount = resultCount;
}
public ArrayList<Album> getAlbums() {
return _albums;
}
public void setAlbums(ArrayList<Album> _albums) {
this._albums = _albums;
}
}
Album类竟被d是要大到在这里张贴,但我已尽可能合理地绘制了元素。
我认为你的意思是deserilize?至于你的映射,我认为应该至少抛出一个异常,但是马上就好像你期望专辑列表映射到“结果”JSON节点,但是没有任何东西可以使这种关联。可以使用log4j将异常记录到其他地方吗? – 2012-04-01 18:22:53
是的,你是对的。但是,我怎样才能将Json“结果”映射到我的课堂上呢? – 2012-04-01 18:37:22