[英]How to parse nested objects in json using Retrofit?
This is reddit/r/pic/.json data. 这是reddit / r / pic / .json数据。 I want to parse preview object and extract image URL. 我想解析预览对象并提取图像URL。
{
"preview": {
"images": [
{
"source": {
"url": "https://i.redditmedia.com/TIqBgNYhZaHMdHN61yUbFPDgDnsFCNkPi6Tb5p2Q-ac.png?s=9ee1ffdf902191de6be14972b7637866",
"width": 772,
"height": 762
},
"resolutions": [
{
"url": "https://i.redditmedia.com/TIqBgNYhZaHMdHN61yUbFPDgDnsFCNkPi6Tb5p2Q-ac.png?fit=crop&crop=faces%2Centropy&arh=2&w=108&s=2d74b9538ff6495f651ce8575baf46b5",
"width": 108,
"height": 106
}
]
}
]
}
}
You can have POJOs
for those fields. 您可以为这些字段使用POJOs
。
Example: 例:
preview here is an object however images is an array/list. 在这里预览是一个对象,但是图像是一个数组/列表。
You create a source class like this 您可以像这样创建一个源类
class Source {
private String url;
private int width;
private int height;
}
you can also see that resolutions array uses same keys/fields as the source class, this becomes more convenient to address 您还可以看到resolutions数组使用与源类相同的键/字段,这将更便于解决
class Images {
private Source source;
private List<Soutrce> resolutions;
}
assuming you have a reddit class for parsing response, your preview will be 假设您有一个reddit类用于解析响应,则预览将是
class Preview {
private List<Images> images;
// .. and some more fields if any
}
class Reddit {
// ...above fields eg: over_18 and approved_by from your example
private Preview preview;
}
so your image will be at preview.images.get(0 /*position here*/).url;
因此您的图片将位于preview.images.get(0 /*position here*/).url;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.