Java 将复杂的JSON解析为列表
我试图显示一些来自url的图像,这些图像给出了JSON响应 我的目标是展示 项目>调整>url 在ListView中的imageview中 答案很复杂:Java 将复杂的JSON解析为列表,java,android,json,Java,Android,Json,我试图显示一些来自url的图像,这些图像给出了JSON响应 我的目标是展示 项目>调整>url 在ListView中的imageview中 答案很复杂: { "count": 84245, "items": [ { "author": "pure julia", "category_id": 66, "content_type"
{
"count": 84245,
"items": [
{
"author": "pure julia",
"category_id": 66,
"content_type": "free",
"cost": 0,
"description": "lanterns, snow, light, winter, night",
"downloads": 227,
"for_adult_only": false,
"id": 195651,
"license": "Unsplash License",
"min_cost_ends_at": "1970-01-01T00:00:00Z",
"rating": 227,
"source_link": "https://unsplash.com/@purejulia",
"tags": [
"lanterns",
"snow",
"light",
"winter",
"night"
],
"uploaded_at": "2020-12-27T07:20:00+0300",
"variations": {
"adapted": {
"resolution": {
"height": 1920,
"width": 1080
},
"size": 250268,
"url": "url"
}
您可以从站点轻松地将其转换为POJO对象(创建列表),您只需将Json粘贴到此处并将其转换,您还可以使用Android Studio提供的一些插件来完成此任务,对于您在中引用的内容,您可以从站点轻松地将其转换为POJO对象(创建列表),您只需将Json粘贴到此处并进行转换,还可以使用Android Studio提供的一些插件来完成此任务,请参见 像这样使用Gson转换器映射类
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Adapted {
@SerializedName("resolution")
@Expose
private Resolution resolution;
@SerializedName("size")
@Expose
private Integer size;
@SerializedName("url")
@Expose
private String url;
public Resolution getResolution() {
return resolution;
}
public void setResolution(Resolution resolution) {
this.resolution = resolution;
}
public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
-----------------------------------com.example.AdaptedLandscape.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class AdaptedLandscape {
@SerializedName("resolution")
@Expose
private Resolution_ resolution;
@SerializedName("size")
@Expose
private Integer size;
@SerializedName("url")
@Expose
private String url;
public Resolution_ getResolution() {
return resolution;
}
public void setResolution(Resolution_ resolution) {
this.resolution = resolution;
}
public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("count")
@Expose
private Integer count;
@SerializedName("items")
@Expose
private List<Item> items = null;
@SerializedName("response_time")
@Expose
private String responseTime;
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
public String getResponseTime() {
return responseTime;
}
public void setResponseTime(String responseTime) {
this.responseTime = responseTime;
}
}
-----------------------------------com.example.Item.java-----------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Item {
@SerializedName("author")
@Expose
private String author;
@SerializedName("category_id")
@Expose
private Integer categoryId;
@SerializedName("content_type")
@Expose
private String contentType;
@SerializedName("cost")
@Expose
private Integer cost;
@SerializedName("description")
@Expose
private String description;
@SerializedName("downloads")
@Expose
private Integer downloads;
@SerializedName("for_adult_only")
@Expose
private Boolean forAdultOnly;
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("license")
@Expose
private String license;
@SerializedName("min_cost_ends_at")
@Expose
private String minCostEndsAt;
@SerializedName("rating")
@Expose
private Integer rating;
@SerializedName("source_link")
@Expose
private String sourceLink;
@SerializedName("tags")
@Expose
private List<String> tags = null;
@SerializedName("uploaded_at")
@Expose
private String uploadedAt;
@SerializedName("variations")
@Expose
private Variations variations;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public Integer getCost() {
return cost;
}
public void setCost(Integer cost) {
this.cost = cost;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getDownloads() {
return downloads;
}
public void setDownloads(Integer downloads) {
this.downloads = downloads;
}
public Boolean getForAdultOnly() {
return forAdultOnly;
}
public void setForAdultOnly(Boolean forAdultOnly) {
this.forAdultOnly = forAdultOnly;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLicense() {
return license;
}
public void setLicense(String license) {
this.license = license;
}
public String getMinCostEndsAt() {
return minCostEndsAt;
}
public void setMinCostEndsAt(String minCostEndsAt) {
this.minCostEndsAt = minCostEndsAt;
}
public Integer getRating() {
return rating;
}
public void setRating(Integer rating) {
this.rating = rating;
}
public String getSourceLink() {
return sourceLink;
}
public void setSourceLink(String sourceLink) {
this.sourceLink = sourceLink;
}
public List<String> getTags() {
return tags;
}
public void setTags(List<String> tags) {
this.tags = tags;
}
public String getUploadedAt() {
return uploadedAt;
}
public void setUploadedAt(String uploadedAt) {
this.uploadedAt = uploadedAt;
}
public Variations getVariations() {
return variations;
}
public void setVariations(Variations variations) {
this.variations = variations;
}
}
-----------------------------------com.example.Original.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Original {
@SerializedName("resolution")
@Expose
private Resolution__ resolution;
@SerializedName("size")
@Expose
private Integer size;
@SerializedName("url")
@Expose
private String url;
public Resolution__ getResolution() {
return resolution;
}
public void setResolution(Resolution__ resolution) {
this.resolution = resolution;
}
public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
-----------------------------------com.example.PreviewSmall.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class PreviewSmall {
@SerializedName("resolution")
@Expose
private Resolution___ resolution;
@SerializedName("size")
@Expose
private Integer size;
@SerializedName("url")
@Expose
private String url;
public Resolution___ getResolution() {
return resolution;
}
public void setResolution(Resolution___ resolution) {
this.resolution = resolution;
}
public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
-----------------------------------com.example.Resolution.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Resolution {
@SerializedName("height")
@Expose
private Integer height;
@SerializedName("width")
@Expose
private Integer width;
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
public Integer getWidth() {
return width;
}
public void setWidth(Integer width) {
this.width = width;
}
}
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Resolution_ {
@SerializedName("height")
@Expose
private Integer height;
@SerializedName("width")
@Expose
private Integer width;
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
public Integer getWidth() {
return width;
}
public void setWidth(Integer width) {
this.width = width;
}
}
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Resolution__ {
@SerializedName("height")
@Expose
private Integer height;
@SerializedName("width")
@Expose
private Integer width;
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
public Integer getWidth() {
return width;
}
public void setWidth(Integer width) {
this.width = width;
}
}
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Resolution___ {
@SerializedName("height")
@Expose
private Integer height;
@SerializedName("width")
@Expose
private Integer width;
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
public Integer getWidth() {
return width;
}
public void setWidth(Integer width) {
this.width = width;
}
}
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Variations {
@SerializedName("adapted")
@Expose
private Adapted adapted;
@SerializedName("adapted_landscape")
@Expose
private AdaptedLandscape adaptedLandscape;
@SerializedName("original")
@Expose
private Original original;
@SerializedName("preview_small")
@Expose
private PreviewSmall previewSmall;
public Adapted getAdapted() {
return adapted;
}
public void setAdapted(Adapted adapted) {
this.adapted = adapted;
}
public AdaptedLandscape getAdaptedLandscape() {
return adaptedLandscape;
}
public void setAdaptedLandscape(AdaptedLandscape adaptedLandscape) {
this.adaptedLandscape = adaptedLandscape;
}
public Original getOriginal() {
return original;
}
public void setOriginal(Original original) {
this.original = original;
}
public PreviewSmall getPreviewSmall() {
return previewSmall;
}
public void setPreviewSmall(PreviewSmall previewSmall) {
this.previewSmall = previewSmall;
}
}
import com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公课改编{
@序列化名称(“决议”)
@暴露
非公开决议;
@序列化名称(“大小”)
@暴露
私有整数大小;
@序列化名称(“url”)
@暴露
私有字符串url;
公开决议{
返回分辨率;
}
公开决议(决议){
这个决议=决议;
}
公共整数getSize(){
返回大小;
}
公共void setSize(整数大小){
这个。大小=大小;
}
公共字符串getUrl(){
返回url;
}
公共void setUrl(字符串url){
this.url=url;
}
}
-----------------------------------com.example.adaptedscape.java-----------------------------------
包com.example;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公共类适应景观{
@序列化名称(“决议”)
@暴露
非公开决议uu决议;
@序列化名称(“大小”)
@暴露
私有整数大小;
@序列化名称(“url”)
@暴露
私有字符串url;
公开决议uGetResolution(){
返回分辨率;
}
公开决议(决议u决议){
这个决议=决议;
}
公共整数getSize(){
返回大小;
}
公共void setSize(整数大小){
这个。大小=大小;
}
公共字符串getUrl(){
返回url;
}
公共void setUrl(字符串url){
this.url=url;
}
}
-----------------------------------com.example.example.java-----------------------------------
包com.example;
导入java.util.List;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公开课范例{
@序列化名称(“计数”)
@暴露
私有整数计数;
@序列化名称(“项目”)
@暴露
私有列表项=空;
@SerializedName(“响应时间”)
@暴露
私有字符串响应时间;
公共整数getCount(){
返回计数;
}
公共无效集合计数(整数计数){
this.count=计数;
}
公共列表getItems(){
退货项目;
}
公共无效集合项(列表项){
这个项目=项目;
}
公共字符串getResponseTime(){
返回响应时间;
}
公共void setResponseTime(字符串responseTime){
this.responseTime=responseTime;
}
}
-----------------------------------com.example.Item.java-----------------------------------
包com.example;
导入java.util.List;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公共类项目{
@序列化名称(“作者”)
@暴露
私有字符串作者;
@SerializedName(“类别id”)
@暴露
私有整数类别ID;
@SerializedName(“内容类型”)
@暴露
私有字符串contentType;
@序列化名称(“成本”)
@暴露
私人整数成本;
@序列化名称(“说明”)
@暴露
私有字符串描述;
@SerializedName(“下载”)
@暴露
私有整数下载;
@序列化名称(“仅适用于成人”)
@暴露
隐匿的布尔无孔;
@序列化名称(“id”)
@暴露
私有整数id;
@序列化名称(“许可证”)
@暴露
私有字符串许可证;
@SerializedName(“最低成本结束于”)
@暴露
私有字符串minCostEndsAt;
@序列化名称(“评级”)
@暴露
私人整数评级;
@SerializedName(“源链接”)
@暴露
私有字符串源链接;
@序列化名称(“标记”)
@暴露
私有列表标签=null;
@SerializedName(“上传地址”)
@暴露
私有字符串上传;
@序列化名称(“变体”)
@暴露
私人变异;
公共字符串getAuthor(){
返回作者;
}
公共void setAuthor(字符串编写器){
this.author=作者;
}
公共整数getCategoryId(){
返回类别ID;
}
public void setCategoryId(整型categoryId){
this.categoryId=categoryId;
}
公共字符串getContentType(){
返回contentType;
}
公共void setContentType(字符串contentType){
this.contentType=contentType;
}
公共整数getCost(){
退货成本;
}
公共无效设置成本(整数成本){
成本=成本;
}
公共字符串getDescription(){
返回说明;
}
公共void集合描述(字符串描述){
this.description=描述;
}
公共整数getDownloads(){
返回下载;
}
公共void setDownloads(整数下载){
this.downloads=下载;
}
公共布尔getforadaltonly(){
有孔回位;
}
public void setForAdultOnly(布尔forAdultOnly){
有孔性=有孔性;
}
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
公共字符串getLicense(){
归还许可证;
}
公共无效设置许可证(字符串许可证){
this.license=许可证;
}
公共字符串getMinCostEndsAt(){
返回minCostEndsAt;
}
公共void setMinCostEndsAt(字符串minCostEndsAt){
this.minCostEndsAt=minCostEndsAt;
}
公共整数getRating(){
回报率;
}
公共无效设置额定值(整数额定值){
这个。评级=评级;
}
公共字符串getSourceLink(){
返回源链接;
}
公共void setSourceLink(字符串源链接){
this.sourceLink=sourceLink;
}
公共列表getTags(){
返回标签;
}
公共无效集合标记(列表标记){
this.tags=标签;
}
公共字符串getUploadedAt(){
返回上传的数据;
}
public void setUploadedAt(字符串上载dat){
this.uploadedAt=uploadedAt;
}
公共变体getVariations(){
回报变化;
}
公共变更(变更){
这个变量=变量
public List<String> GetImageUrls() throws IOException {
String requestUrl = "https://api.wallpaperscraft.com/images?screen[width]=1080&screen[height]=1920&sort=date&offset=0&types[]=free&types[]=private";
ObjectMapper mapper = new ObjectMapper();
List<String> urls = new ArrayList<>();
Map<String, Object> variation;
Map<String, Object> landscape;
Map<String, Object> responseMap = mapper.readValue(new URL(requestUrl), new TypeReference<Map<String, Object>>() {
});
for (Object item : (List) responseMap.get("items")) {
if (item instanceof Map) {
variation = (Map<String, Object>) ((Map) item).get("variations");
landscape = (Map<String, Object>) variation.get("adapted_landscape");
urls.add((String) landscape.get("url"));
}
}
return urls;
}