首页 > 解决方案 > JSON响应中同一对象的字符串和布尔值

问题描述

我有一个“rest api”,它使我成为我的 CMS 插件,我在 android 中用于我的应用程序。

当图像存在时,它返回 a string,当没有图像时,它返回 a boolean

如果图像存在,则在 json 中为:

 {  
   "events":[  
      {  
         "id":4651,
         "title":"Test title",
         "description":"testttt",
         "image": {
            url: "https://myhost.tv/wp-content/uploads/2019/07/event2.jpg",
            id: "4652"}
       }
       ]
 }

如果我的帖子没有图片,则json如下:

{  
   "events":[  
      {  
         "id":4652,
         "title":"Test title 2",
         "description":"testttt",
         "image":false
       }
       ]
 }

我的recyclerview模型是:

data class EventosFeed(
    val events: Array<Events>
)

data class Events (

    val id : Int,
    val title : String,
    val image : Image? = null,
    .
    .
    .
)


data class Image (
    val url : String,
    val id : Int,
)

如果帖子有图片,没问题,但如果我的帖子没有图片,我就有问题,因为它是模型中的字符串。模型如何解决这个问题。

标签: jsonandroid-studiokotlin

解决方案


推荐阅读