3 回答

TA貢獻1798條經(jīng)驗 獲得超7個贊
json 不是很干凈,我建議你重新設計這個 json 字符串的生成方式,但是如果你仍然想在不重新處理 json 的情況下獲得這些值,你需要將你的Items類更改為:
`public class items {
public String name;
public String featured;
public Item item;
}`
然后您需要創(chuàng)建另一個名為Item的類
`public class Item{
public String image;
public Image images;
}`
您還需要創(chuàng)建一個Image類,如下所示:
`public class Image {
public String transparent;
public String transparent_blank;
public String transparent_dark;
}`
然后您可以在循環(huán)中記錄這些值:
`Datos datosFinal = gson.fromJson(response_jSon, Datos.class);
for (int i=0; i<datosFinal.items.size(); i++){
Log.d("msg_2", datosFinal.items.get(i).item.images.transparent);
Log.d("msg_2", datosFinal.items.get(i).item.images.transparent_blank);
Log.d("msg_2", datosFinal.items.get(i).item.images.transparent_dark);
}`
雖然這可能有效,但我強烈建議您重新編寫 json 并使其更易于維護。

TA貢獻1898條經(jīng)驗 獲得超8個贊
你必須使用
public class items {
public String name;
@Expose(serialize = false, deserialize = false)
public String featured;
@Expose(serialize = false, deserialize = false)
public item mItem;
}
public class item {
@Expose(serialize = false, deserialize = false)
public String image;
@Expose(serialize = false, deserialize = false)
public images mImages;
}
public class images {
@Expose(serialize = false, deserialize = false)
public String transparent;
@Expose(serialize = false, deserialize = false)
public String transparent_blank;
@Expose(serialize = false, deserialize = false)
public String transparent_dark;
}
您可以使用http://www.jsonschema2pojo.org/生成 pojo

TA貢獻1836條經(jīng)驗 獲得超5個贊
您還必須創(chuàng)建以下類以獲得透明、透明空白和透明暗的價值
Images.java
項目.java
替換這個類:
public class Items{
private Item item;
private String name;
private String featured;
public Item getItem ()
{
return item;
}
public void setItem (Item item)
{
this.item = item;
}
public String getName ()
{
return name;
}
public void setName (String name)
{
this.name = name;
}
public String getFeatured ()
{
return featured;
}
public void setFeatured (String featured)
{
this.featured = featured;
}
}
添加這個類:
public class Images{
private String transparent_blank;
private String transparent_dark;
private String transparent;
public String getTransparent_blank ()
{
return transparent_blank;
}
public void setTransparent_blank (String transparent_blank)
{
this.transparent_blank = transparent_blank;
}
public String getTransparent_dark ()
{
return transparent_dark;
}
public void setTransparent_dark (String transparent_dark)
{
this.transparent_dark = transparent_dark;
}
public String getTransparent ()
{
return transparent;
}
public void setTransparent (String transparent)
{
this.transparent = transparent;
}
}
另外,添加這個類:
public class Item {
private Images images;
private String image;
public Images getImages ()
{
return images;
}
public void setImages (Images images)
{
this.images = images;
}
public String getImage ()
{
return image;
}
public void setImage (String image)
{
this.image = image;
}
}
現(xiàn)在,您將使用 getter 方法獲取值。
添加回答
舉報