繁体   English   中英

如何生成 JSON 响应的 POJO class

[英]How to generate POJO class of JSON response

Hello all i am using PixaBay API to fetch images in my app.I am using url https://pixabay.com/api/?key=My-KEY .I am getting response like below how can I make POJO class from below response .

 {
"total":1261984,
"totalHits":500,
"hits":[
    {
        "id":5265194,
        "pageURL":"https://pixabay.com/photos/lein-staudenlein-blue-flax-flower-5265194/",
        "type":"photo",
        "tags":"lein, staudenlein, blue flax",
        "previewURL":"https://cdn.pixabay.com/photo/2020/06/06/04/20/lein-5265194_150.jpg",
        "previewWidth":150,
        "previewHeight":100,
        "webformatURL":"https://pixabay.com/get/53e2d3464b5ba814f1dc8460962931771d3cdae5504c704c7c2e73d5964fc05e_640.jpg",
        "webformatWidth":640,
        "webformatHeight":427,
        "largeImageURL":"https://pixabay.com/get/53e2d3464b5ba814f6da8c7dda7936781c37dde153526c4870267adc964cc75dbe_1280.jpg",
        "imageWidth":6240,
        "imageHeight":4160,
        "imageSize":5132057,
        "views":9106,
        "downloads":8127,
        "favorites":25,
        "likes":77,
        "comments":68,
        "user_id":10327513,
        "user":"NickyPe",
        "userImageURL":"https://cdn.pixabay.com/user/2020/06/08/09-39-40-606_250x250.jpg"
    },
    {
        "id":5255326,
        "pageURL":"https://pixabay.com/photos/landscape-fantasy-sky-clouds-5255326/",
        "type":"photo",
        "tags":"landscape, fantasy, sky",
        "previewURL":"https://cdn.pixabay.com/photo/2020/06/03/15/20/landscape-5255326_150.jpg",
        "previewWidth":150,
        "previewHeight":100,
        "webformatURL":"https://pixabay.com/get/53e2d0464950aa14f1dc8460962931771d3cdae5504c704c7c2e73d5964fc05e_640.jpg",
        "webformatWidth":640,
        "webformatHeight":427,
        "largeImageURL":"https://pixabay.com/get/53e2d0464950aa14f6da8c7dda7936781c37dde153526c4870267adc964cc75dbe_1280.jpg",
        "imageWidth":7087,
        "imageHeight":4724,
        "imageSize":3912235,
        "views":29283,
        "downloads":24114,
        "favorites":80,
        "likes":174,
        "comments":108,
        "user_id":3764790,
        "user":"enriquelopezgarre",
        "userImageURL":"https://cdn.pixabay.com/user/2020/06/03/11-05-03-625_250x250.jpg"
    }
  ]
} 

在这里,我只想从上面的响应中获取预览 URL。我怎样才能制作 POJO class 。有人请帮助我,任何帮助将不胜感激。

谢谢

你可以使用这个网站来生成 pojo 类。 这是链接http://www.jsonschema2pojo.org/

执行以下操作:

public class Pixabay{
   public int total;
   public int totalHits;
   public List<Hits> hits;

  // inner class Hits with all attributes 
   public class Hits{
    public int id;
    //..... and so on

     }
  }

我希望你得到图片? 你必须通过检查它们的值来正确地 map 它们。

干杯!

你的 class 会像

data class pixBit(val hints:List<pixBitList>)
data class pixBitList(val previewURL:String)

Map 您对第一个 pojo class 的响应,通过从映射数据访问提示列表,您可以获取每个 previewUrl

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM