如何通过具有多个级别数组的 Gson 对象

我正在尝试使用Gson库获取json中的参数。 这个想法是捕捉和。但我不知道如何获得这些值,目前我创建了以下内容: items>item>image>images>transparenttransparenttransparent_blanktransparent_dark


json


  "date_layout":"day-month-year",

  "lastupdate":1547596830,

    "items":[{

      "name":"Cleans Cuts",

      "featured":"true",

      "item":{

        "image":"http:www.domain.com/unwanted_image.jpg",

           "images":{

              "transparent":"http:www.domain.com/desired_image1.jpg",

              "transparent_blank":"http:www.domain.com/desired_image2.jpg",

              "transparent_dark":"http:www.domain.com/desired_image3.jpg"

             }

          }

    },

    {

      "name":"Cleans Cuts",

      "featured":"true",

      "item":{

        "image":"http:www.domain.com/unwanted_image.jpg",

           "images":{

              "transparent":"http:www.domain.com/desired_image1.jpg",

              "transparent_blank":"http:www.domain.com/desired_image2.jpg",

              "transparent_dark":"http:www.domain.com/desired_image3.jpg"

             }

          }

    }]

}

。主要活动


public class MainActivity extends AppCompatActivity {

@Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


String url = "http://www.example.com/file.json";


Request request = new Request.Builder()

       .url(url)

       .build();


Response response = client.newCall(request).execute();

       if(response.isSuccessful()){

          String response_jSon = response.body().string();

          Gson gson = new Gson();

          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).name);

              }

}

而且我只能访问您在 .MainActivity 日志中看到的项目。


Json 的类对象


达托斯.java


public class Datos {

    public String date_layout;

    public Int lastupdate;

     List<items> items;

}

项目.java


public class items {

    public String name;

}


拉丁的传说
浏览 155回答 3
3回答

元芳怎么了

json 不是很干净,我建议你重新设计这个 json 字符串的生成方式,但是如果你仍然想在不重新处理 json 的情况下获得这些值,你需要将你的Items类更改为:`public class items {&nbsp; &nbsp; public String name;&nbsp; &nbsp; public String featured;&nbsp; &nbsp; public Item item;}`&nbsp;然后您需要创建另一个名为Item的类`public class Item{&nbsp; &nbsp; public String image;&nbsp; &nbsp; public Image images;}`您还需要创建一个Image类,如下所示:`public class Image {&nbsp; &nbsp; public String transparent;&nbsp; &nbsp; public String transparent_blank;&nbsp; &nbsp; public String transparent_dark;}`然后您可以在循环中记录这些值:`Datos datosFinal = gson.fromJson(response_jSon, Datos.class);&nbsp;for (int i=0; i<datosFinal.items.size(); i++){&nbsp; &nbsp; &nbsp; &nbsp; Log.d("msg_2", datosFinal.items.get(i).item.images.transparent);&nbsp; &nbsp; &nbsp; &nbsp; Log.d("msg_2", datosFinal.items.get(i).item.images.transparent_blank);&nbsp; &nbsp; &nbsp; &nbsp; Log.d("msg_2", datosFinal.items.get(i).item.images.transparent_dark);}`虽然这可能有效,但我强烈建议您重新编写 json 并使其更易于维护。

汪汪一只猫

你必须使用public class items {&nbsp; &nbsp; public String name;&nbsp; &nbsp; @Expose(serialize = false, deserialize = false)&nbsp; &nbsp; public String featured;&nbsp; &nbsp; @Expose(serialize = false, deserialize = false)&nbsp; &nbsp; public item mItem;}public class item {&nbsp; &nbsp; @Expose(serialize = false, deserialize = false)&nbsp; &nbsp; public String image;&nbsp; &nbsp; @Expose(serialize = false, deserialize = false)&nbsp; &nbsp; public images mImages;}public class images {&nbsp; &nbsp; @Expose(serialize = false, deserialize = false)&nbsp; &nbsp; public String transparent;&nbsp; &nbsp; @Expose(serialize = false, deserialize = false)&nbsp; &nbsp; public String transparent_blank;&nbsp; &nbsp; @Expose(serialize = false, deserialize = false)&nbsp; &nbsp; public String transparent_dark;}您可以使用http://www.jsonschema2pojo.org/生成 pojo

一只甜甜圈

您还必须创建以下类以获得透明、透明空白和透明暗的价值Images.java项目.java替换这个类:public class Items{private Item item;private String name;private String featured;public Item getItem (){&nbsp; &nbsp; return item;}public void setItem (Item item){&nbsp; &nbsp; this.item = item;}public String getName (){&nbsp; &nbsp; return name;}public void setName (String name){&nbsp; &nbsp; this.name = name;}public String getFeatured (){&nbsp; &nbsp; return featured;}public void setFeatured (String featured){&nbsp; &nbsp; this.featured = featured;}}添加这个类:public class Images{private String transparent_blank;private String transparent_dark;private String transparent;public String getTransparent_blank (){&nbsp; &nbsp; return transparent_blank;}public void setTransparent_blank (String transparent_blank){&nbsp; &nbsp; this.transparent_blank = transparent_blank;}public String getTransparent_dark (){&nbsp; &nbsp; return transparent_dark;}public void setTransparent_dark (String transparent_dark){&nbsp; &nbsp; this.transparent_dark = transparent_dark;}public String getTransparent (){&nbsp; &nbsp; return transparent;}public void setTransparent (String transparent){&nbsp; &nbsp; this.transparent = transparent;}}另外,添加这个类:public class Item {private Images images;private String image;public Images getImages (){&nbsp; &nbsp; return images;}public void setImages (Images images){&nbsp; &nbsp; this.images = images;}public String getImage (){&nbsp; &nbsp; return image;}public void setImage (String image){&nbsp; &nbsp; this.image = image;}}&nbsp; &nbsp;&nbsp;现在,您将使用 getter 方法获取值。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java