2012-04-01 58 views
1

我试图将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是要大到在这里张贴,但我已尽可能合理地绘制了元素。

+0

我认为你的意思是deserilize?至于你的映射,我认为应该至少抛出一个异常,但是马上就好像你期望专辑列表映射到“结果”JSON节点,但是没有任何东西可以使这种关联。可以使用log4j将异常记录到其他地方吗? – 2012-04-01 18:22:53

+0

是的,你是对的。但是,我怎样才能将Json“结果”映射到我的课堂上呢? – 2012-04-01 18:37:22

回答

0

尝试使用JAXB annotations或使用Jersey进行JSON映射。本手册可能对您有用:link

1

注:我是EclipseLink JAXB (MOXy)铅和JAXB 2 (JSR-222)专家小组的成员。

下面是它如何与莫西通过利用JAXB注释来完成:

AlbumInfo

package forum9966753; 

import java.io.Serializable; 
import java.util.ArrayList; 
import javax.xml.bind.annotation.*; 

@XmlType(propOrder={"resultCount", "albums"}) 
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; 
    } 

    @XmlElement(name="results") 
    public ArrayList<Album> getAlbums() { 
     return _albums; 
    } 

    public void setAlbums(ArrayList<Album> _albums) { 
     this._albums = _albums; 
    } 

} 

专辑

下面是你的Album类的缩小版本:

package forum9966753; 

public class Album { 

    private String wrapperType; 

    public String getWrapperType() { 
     return wrapperType; 
    } 

    public void setWrapperType(String wrapperType) { 
     this.wrapperType = wrapperType; 
    } 

} 

jaxb.properties

要指定莫西为您的JAXB提供者,你需要在同一个包添加一个名为jaxb.properties文件作为域类具有以下条目:

javax.xml.bind.context.factory = org.eclipse.persistence.jaxb.JAXBContextFactory 

演示

package forum9966753; 

import java.io.InputStream; 
import java.net.*; 
import java.util.List; 
import javax.xml.bind.*; 
import javax.xml.transform.stream.StreamSource; 
import org.example.Customer; 

public class JavaSEClient { 

    private static final String MEDIA_TYPE = "application/json"; 

    public static void main(String[] args) throws Exception { 
     String uri = "http://itunes.apple.com/search?term=marc+jordan&media=music&entity=album"; 
     URL url = new URL(uri); 
     HttpURLConnection connection = 
      (HttpURLConnection) url.openConnection(); 
     connection.setRequestMethod("GET"); 
     connection.setRequestProperty("Accept", MEDIA_TYPE); 

     JAXBContext jc = JAXBContext.newInstance(AlbumInfo.class); 

     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     unmarshaller.setProperty("eclipselink.media-type", MEDIA_TYPE); 
     unmarshaller.setProperty("eclipselink.json.include-root", false); 
     InputStream xml = connection.getInputStream(); 
     AlbumInfo albumInfo = unmarshaller.unmarshal(new StreamSource(xml), AlbumInfo.class).getValue(); 
     connection.disconnect(); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.setProperty("eclipselink.media-type", MEDIA_TYPE); 
     marshaller.setProperty("eclipselink.json.include-root", false); 
     marshaller.marshal(albumInfo, System.out); 
    } 

} 

输出

以下是运行演示代码的输出。由于样本域模型只包含一对属性,所以输出比输出小得多。 JAXB映射可以很容易地应用于映射文档的其余部分。

{ 
    "resultCount" : 49, 
    "results" : [ { 
     "wrapperType" : "collection" 
    } ] 
} 

更多信息

2

如果你还没有,你需要的Restlet的JacksonConverter添加到已注册转换器的列表:

List<ConverterHelper> converters = Engine.getInstance().getRegisteredConverters(); 
    converters.add(new JacksonConverter()); 

一个d,当然,将org.restlet.ext.jackson.jar添加到您的构建路径中。

0

最近我不得不开发一个带有Restlet框架的Android应用程序,我花了很多时间来理解如何反序列化一个JSONObject。 在这里,我将解释我的方法。 我还出版了一本完整的Android应用程序在GitHub上的位置:
https://github.com/alchimya/android-restlet

的Restlet 2.3.2包括GSON库。使用Gson对资源进行映射和反序列化非常简单。

1)地图一类的实体基础如下:

import com.google.gson.annotations.SerializedName; 
import java.io.Serializable; 

public class Album implements Serializable { 

    @SerializedName("wrapperType") 
    private String wrapperType; 

    @SerializedName("collectionType") 
    private String collectionType; 

    @SerializedName("artistId") 
    private String artistId; 

    public String getWrapperType() { 
     return wrapperType; 
    } 
    public void setWrapperType(String wrapperType) { 
     this.wrapperType = wrapperType; 
    } 

    public String getCollectionType() { 
     return collectionType; 
    } 
    public void setCollectionType(String collectionType) { 
     this.collectionType = collectionType; 
    } 

    public String getArtistId() { 
     return artistId; 
    } 
    public void setArtistId(String artistId) { 
     this.artistId = artistId; 
    } 

    ...... 
    ...... 
    ...... 
} 

注:在前面的类中的每个属性都有一个注释(@SerializedName)来映射相应的JSON场。欲了解更多信息请参见本教程:
http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html

2)用列表属性创建一个类:

public class Albums { 
    public List<Album> results; 
} 

3)服用含有的Restlet

String uri="http://itunes.apple.com/search?term=marc+jordan&media=music&entity=album"; 

ClientResource resource = new ClientResource(url); 
Representation rep = resource.get(); 

JsonRepresentation represent = new JsonRepresentation(rep); 
JSONObject jsonobject = represent.getJsonObject(); 
String jsonText = jsonobject.toString(); 

Gson gson = new Gson(); 
Albums response = gson.fromJson(jsonText, Albums.class); 

进入response.results您的资源将会有所有的反序列化项目。