简体   繁体   English

一种类型的Gson反序列化结果为空

[英]Gson deserialisation result is null for one type

I'm getting a response which consists of two types of objects : Pagination and a List of ArtistSearch . 我收到一个包含两种类型的对象的响应: PaginationArtistSearch列表。 The model I'm using for the deserialization is : 我用于反序列化的模型是:

public class ArtistSearchResults {
List<ArtistSearch> artistSearchList;
Pagination pagination;

// getters and setters...

}

And I deserialize here : 我在这里反序列化:

            Gson gson = new Gson();
            ArtistSearchResults results = gson.fromJson(response.toString(), ArtistSearchResults.class);
            List<ArtistSearch> artistSearchList = results.getArtistSearchList();
            Pagination pagination = results.getPagination();

            Log.i(TAG, "onSuccess Pagination size == " + pagination.getItems() );
            Log.i(TAG, "onSuccess RESULTS size == " + artistSearchList.get(0).getTitle() );

and although I get correctly the response for the Pagination object I'm always getting null for the artistSearchList. 尽管我正确地获得了Pagination对象的响应,但artistSearchList始终为null。

I'm not getting any error messages so I'm not sure where the error is. 我没有收到任何错误消息,所以我不确定错误在哪里。 The kind of results I'm getting looks like this : 我得到的结果看起来像这样:

07-10 06:11:06.726 6954-6954/jb.ti.discogsball I/SEARCHHANDLER: onSuccess Search-Artist response = {"pagination":{"per_page":5,"pages":3,"page":1,"urls":{"last":"https:\/\/api.discogs.com\/database\/search?q=savage+republic&per_page=5&type=artist&page=3","next":"https:\/\/api.discogs.com\/database\/search?q=savage+republic&per_page=5&type=artist&page=2"},"items":14},"results": [{"thumb":"https:\/\/api-img.discogs.com\/RAHE1vqHWb1xwPUv5y2Q1v5g4Yo=\/150x150\/smart\/filters:strip_icc():format(jpeg):mode_rgb():quality(40)\/discogs-images\/A-121133-1262786660.jpeg.jpg","title":"Savage Republic","uri":"\/artist\/121133-Savage-Republic","resource_url":"https:\/\/api.discogs.com\/artists\/121133","type":"artist","id":121133},{"thumb":"https:\/\/api-img.discogs.com\/GYkPyAYZEcFM0bzyjnBple7P3Yw=\/150x150\/smart\/filters:strip_icc():format(jpeg):mode_rgb():quality(40)\/discogs-images\/A-307086-1160815350.jpeg.jpg","title":"Bruce Licher","uri":"\/artist\/307086-Bruce-Licher","resource_url":"https:\/\/api.discogs.com\/artists\/307086","type":"artist","id":307086},{"thumb":"https:\/\/api-img.discogs.com\/XHvbh885CZ2uTcq7WT3ph0n0gMs=\/150x150\/smart\/filters:strip_icc():format(jpeg):mode_rgb():quality(40)\/discogs-images\/A-71224-1334785233.jpeg.jpg","title":"Medicine (2)","uri":"\/artist\/71224-Medicine-2","resource_url":"https:\/\/api.discogs.com\/artists\/71224","type":"artist","id":71224},{"thumb":"","title":"Val Haller","uri":"\/artist\/492425-Val-Haller","resource_url":"https:\/\/api.discogs.com\/artists\/492425","type":"artist","id":492425},{"thumb":"","title":"Ramona Clarke","uri":"\/artist\/1957827-Ramona-Clarke","resource_url":"https:\/\/api.discogs.com\/artists\/1957827","type":"artist","id":1957827}]}
07-10 06:11:06.732 6954-6954/jb.ti.discogsball I/SEARCHHANDLER: onSuccess Pagination size == 14

and as you can see I get a value for the Pagination object but I get a NPE for the artistSearchList. 如您所见,我获得了Pagination对象的值,但获得了artistSearchList的NPE。

The model I'm using for ArtistSearch is : 我用于ArtistSearch的模型是:

public class ArtistSearch {
int id;
String title;
String type;
String resource_url;
String uri;
String thumb;

// getters and setters ...

This is the text from the response that parses to null : 这是响应中解析为null的文本:

"results":[{"thumb":"https://api-img.discogs.com/RAHE1vqHWb1xwPUv5y2Q1v5g4Yo=/150x150/smart/filters:strip_icc():format(jpeg):mode_rgb():quality(40)/discogs-images/A-121133-1262786660.jpeg.jpg","title":"Savage Republic","uri":"/artist/121133-Savage-Republic","resource_url":"https://api.discogs.com/artists/121133","type":"artist","id":121133},{"thumb":"https://api-img.discogs.com/GYkPyAYZEcFM0bzyjnBple7P3Yw=/150x150/smart/filters:strip_icc():format(jpeg):mode_rgb():quality(40)/discogs-images/A-307086-1160815350.jpeg.jpg","title":"Bruce Licher","uri":"/artist/307086-Bruce-Licher","resource_url":"https://api.discogs.com/artists/307086","type":"artist","id":307086},{"thumb":"https://api-img.discogs.com/XHvbh885CZ2uTcq7WT3ph0n0gMs=/150x150/smart/filters:strip_icc():format(jpeg):mode_rgb():quality(40)/discogs-images/A-71224-1334785233.jpeg.jpg","title":"Medicine (2)","uri":"/artist/71224-Medicine-2","resource_url":"https://api.discogs.com/artists/71224","type":"artist","id":71224},{"thumb":"" “结果”:[{ “拇指”:“https://api-img.discogs.com/RAHE1vqHWb1xwPUv5y2Q1v5g4Yo=/150x150/smart/filters:strip_icc():format(jpeg):mode_rgb():quality(40)/ discogs-images / A-121133-1262786660.jpeg.jpg“,” title“:”野蛮共和国“,” uri“:” / artist / 121133-Savage-Republic“,” resource_url“:” https:// api。 discogs.com/artists/121133","type":"artist","id":121133},{"thumb":"https://api-img.discogs.com/GYkPyAYZEcFM0bzyjnBple7P3Yw=/150x150/smart/filters :strip_icc():format(jpeg):mode_rgb():quality(40)/discogs-images/A-307086-1160815350.jpeg.jpg“,”标题“:” Bruce Licher“,” uri“:” /艺术家/307086-Bruce-Licher","re​​source_url":"https://api.discogs.com/artists/307086","type":"artist","id":307086},{"thumb":"https ://api-img.discogs.com/XHvbh885CZ2uTcq7WT3ph0n0gMs=/150x150/smart/filters:strip_icc():格式(JPEG):mode_rgb():质量(40)/discogs-images/A-71224-1334785233.jpeg。 jpg“,” title“:”医学(2)“,” uri“:” / artist / 71224-Medicine-2“,” resource_url“:” https://api.discogs.com/artists/71224“,”键入 “:” 艺术家”, “ID”:71224},{ “拇指”: “” ,"title":"Val Haller","uri":"/artist/492425-Val-Haller","resource_url":"https://api.discogs.com/artists/492425","type":"artist","id":492425},{"thumb":"","title":"Ramona Clarke","uri":"/artist/1957827-Ramona-Clarke","resource_url":"https://api.discogs.com/artists/1957827","type":"artist","id":1957827}] ,“标题”:“ Val Haller”,“ uri”:“ / artist / 492425-Val-Haller”,“ resource_url”:“ https://api.discogs.com/artists/492425”,“类型”:” artist“,” id“:492425},{” thumb“:”“,” title“:” Ramona Clarke“,” uri“:” / artist / 1957827-Ramona-Clarke“,” resource_url“:” https:/ /api.discogs.com/artists/1957827","type":"artist","id":1957827}]

Could you please replace the following variable declaration in ArtistSearchResults.java : 您能否在ArtistSearchResults.java替换以下变量声明:

private List<ArtistSearch> artistSearchList;

by this: 这样:

@SerializedName("results")
private List<ArtistSearch> artistSearchList;

and see the results? 看看结果吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM