简体   繁体   中英

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"
    }
  ]
} 

Here I only want to fetch preview URL from above response.How can I make POJO class for the same.Someone please help me out any help would be appreciated.

THANKS

You can use this website to generate pojo classes. Here is the link http://www.jsonschema2pojo.org/

Do something like:

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

     }
  }

I hope u get the picture? U have to map them correctly by checking their values.

Cheers!

Your class will be like

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

Map your response to first pojo class and by accessing the hints list from the mapped data you can fetch each previewUrl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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