如何为lastfm相册构建pojo类.search json响应解析
How to build an pojo class for a lastfm album.search json response parse
我这里有Json
http://ws.audioscrobbler.com/2.0/?method=album.search&专辑=+精华+的+查理+帕克&limit=1&api_key=26980b71e5c813da2c7e0156afdddd4f&format=json
我制作了这个pojo类来解析它
import java.util.List;
public class LastFmAlbumInfo {
public Results results;
public class Results {
QueryInfo queryInfo;
String totalResults;
String startIndex;
String itemsPerPage;
AlbumMatches albumMatches;
Attribute attribute;
public class QueryInfo {
String text;
String role;
String searchTerms;
String startPage;
}
public class AlbumMatches {
List<Album> albums;
public class Album {
String name;
String artist;
String url;
List<Image> images;
String streamable;
String mbid;
public class Image {
String imageUrl;
String imageSizeDescription;
}
}
}
public class Attribute {
String _for;
}
}
}
当我引用一个子属性时,例如
response.results.totalResults
我收到这个错误
java.lang.NullPointerException:尝试从空对象引用上的字段"java.util.List com.example.michael.musicplayer5.LastFmAlbumInfo$Results$AlbumMatches.albums"读取
我不确定我的pojo课出了什么问题。
我正在使用Volley进行GET请求
String requestUrl = getUrl(albumObject.albumTitle);
getRequestQueue();
GsonRequest<LastFmAlbumInfo> myReq = new GsonRequest<>(
Request.Method.GET,
requestUrl,
LastFmAlbumInfo.class,
null,
createMyReqSuccessListener(requestUrl, albumObject),
createMyReqErrorListener(requestUrl));
mRequestQueue.add(myReq);
我搞定了!
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class LastFmAlbumInfo {
@SerializedName("results")
public Results results;
public class Results {
//@SerializedName(“opensearch:Query”)
//QueryInfo opensearch;
@SerializedName("opensearch:totalResults")
String totalResults;
@SerializedName("opensearch:startIndex")
String startIndex;
@SerializedName("opensearch:itemsPerPage")
String itemsPerPage;
@SerializedName("albummatches")
AlbumMatches albumMatches;
@SerializedName("@attr")
Attribute attribute;
public class QueryInfo {
@SerializedName("#text")
String text;
@SerializedName("role")
String role;
@SerializedName("searchTerms")
String searchTerms;
@SerializedName("startPage")
String startPage;
}
public class AlbumMatches {
@SerializedName("album")
List<Album> albums;
public class Album {
@SerializedName("name")
String name;
@SerializedName("artist")
String artist;
@SerializedName("url")
String url;
@SerializedName("image")
List<Image> images;
@SerializedName("streamable")
String streamable;
@SerializedName("mbid")
String mbid;
public class Image {
@SerializedName("#text")
String imageUrl;
@SerializedName("size")
String imageSizeDescription;
}
}
}
public class Attribute {
@SerializedName("for")
String _for;
}
}
相关文章:
- jQuery匹配JSON对象的部分文本
- 在循环中分配json值时,值被覆盖
- 需要帮助设置json数组
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 使用JQuery解析JSON嵌套数组
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- 如何使用json将对象列表从java转换为javascript
- 如何使用 node.js 比较两个 json 数组
- 如何将JSON数据导入我的ejs模板
- 区分JSON中的矩阵和列表列表
- 不显示带有本地json文件数据的谷歌地图脚本
- 如何将json数据显示为html
- 如何为json对象中的段发送array[]
- JSON and jQuery Search
- 我应该使用哪种 http://search.twitter.com/search.json 来获得Twitter的回复
- 如何为lastfm相册构建pojo类.search json响应解析
- 搜索Search: angular中的json数据