【发布时间】:2017-12-13 17:22:13
【问题描述】:
所以,我正在尝试通过构建一个应用程序来学习 android,在该应用程序中我使用 lastFm api 来获取类似的歌曲。我将 retrofit2 与 Gson 和 rxJava 一起使用。
要读取 json 响应,我使用 Pojo 类。要首先获得类似的曲目,我必须搜索它(第一次 api 调用),然后我必须根据用户选择的那个来调用 api。
一些 POJO 在这些调用之间是通用的,所以我不必复制它们。
我的问题是,在第一次调用中,响应包含一个艺术家作为字符串(只有名称),而在第二个响应中,艺术家是一个对象。
显然,当我尝试从第一个响应中读取时,Gson 期待一个“艺术家”对象,但它却得到了一个字符串。作为“Track”,我如何将它们都实现到一个 POJO 中?
这是我的代码,我希望我很清楚(当我试图解释时遇到问题......,好吧,我的问题)
public class Track implements Parcelable{
@SerializedName("name")
@Expose
private String name;
@SerializedName("playcount")
@Expose
private Integer mPlaycount;
@SerializedName("artist")
@Expose
private Artist artist; // If i make this a String, the first call works
@SerializedName("url")
@Expose
private String url;
@SerializedName("streamable")
@Expose
private Streamable streamable;
@SerializedName("listeners")
@Expose
private String listeners;
@SerializedName("match")
@Expose
private String match;
@SerializedName("image")
@Expose
private List<Image> image = new ArrayList<>();
@SerializedName("mbid")
@Expose
private String mbid;
@Expose
private String mediumImage;
// Getters, setter, parcelable methods etc.
JSon 第一响应示例(不是完整响应,只有一个结果) 响应来自 lastFm api
{
"results": {
"opensearch:Query": {
"#text": "",
"role": "request",
"startPage": "1"
},
"opensearch:totalResults": "1637",
"opensearch:startIndex": "0",
"opensearch:itemsPerPage": "30",
"trackmatches": {
"track": [
{
"name": "Ich tu dir weh",
"artist": "Rammstein",
"url": "https://www.last.fm/music/Rammstein/_/Ich+tu+dir+weh",
"streamable": "FIXME",
"listeners": "198819",
"image": [
{
"#text": "https://lastfm-img2.akamaized.net/i/u/34s/1a82cbe48f604d4c8325122b09d310ed.png",
"size": "small"
},
{
"#text": "https://lastfm-img2.akamaized.net/i/u/64s/1a82cbe48f604d4c8325122b09d310ed.png",
"size": "medium"
},
{
"#text": "https://lastfm-img2.akamaized.net/i/u/174s/1a82cbe48f604d4c8325122b09d310ed.png",
"size": "large"
},
{
"#text": "https://lastfm-img2.akamaized.net/i/u/300x300/1a82cbe48f604d4c8325122b09d310ed.png",
"size": "extralarge"
}
],
"mbid": "1752a3db-918e-43ba-b7ac-63bcc9a6433c"
}
]
},
"@attr": {}
}
}
JSon 第二个响应样本(不是完整响应,只有一个结果) 响应来自 lastFm api
{
"similartracks": {
"track": [
{
"name": "Waidmanns Heil",
"playcount": 1806826,
"mbid": "e1e060e0-73e7-4939-ac80-635e8cba45a2",
"match": 1,
"url": "https://www.last.fm/music/Rammstein/_/Waidmanns+Heil",
"streamable": {
"#text": "0",
"fulltrack": "0"
},
"duration": 213,
"artist": {
"name": "Rammstein",
"mbid": "b2d122f9-eadb-4930-a196-8f221eeb0c66",
"url": "https://www.last.fm/music/Rammstein"
},
"image": [
{
"#text": "https://lastfm-img2.akamaized.net/i/u/34s/1a82cbe48f604d4c8325122b09d310ed.png",
"size": "small"
},
{
"#text": "https://lastfm-img2.akamaized.net/i/u/64s/1a82cbe48f604d4c8325122b09d310ed.png",
"size": "medium"
},
{
"#text": "https://lastfm-img2.akamaized.net/i/u/174s/1a82cbe48f604d4c8325122b09d310ed.png",
"size": "large"
},
{
"#text": "https://lastfm-img2.akamaized.net/i/u/300x300/1a82cbe48f604d4c8325122b09d310ed.png",
"size": "extralarge"
},
{
"#text": "https://lastfm-img2.akamaized.net/i/u/300x300/1a82cbe48f604d4c8325122b09d310ed.png",
"size": "mega"
},
{
"#text": "https://lastfm-img2.akamaized.net/i/u/300x300/1a82cbe48f604d4c8325122b09d310ed.png",
"size": ""
}
]
}
],
"@attr": {
"artist": "Rammstein"
}
}
}
提前感谢您的回复。 p.s.堆栈溢出中的第一个问题...耶!
【问题讨论】:
标签: java android json retrofit2 gson