簡體   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