我想使用Gson库解析Json文件。
由于某种原因,gson.fromJson()方法返回null。
我已经在Android Studio中使用JSON到Kotlin插件创建了数据类,并使用Okhttp 4.9.0检索数据。
方法:
val request = Request.Builder().url(url).build()
val client = OkHttpClient()
client.newCall(request).enqueue(object: Callback {
override fun onFailure(call: Call, e: IOException) {
println("Failed your request")
}
override fun onResponse(call: Call, response: Response) {
val body = response.body?.string()
println(body)
val gson = GsonBuilder().create()
val result = gson.fromJson(body, YoutubeAPIModel::class.java)
}
})
YouTube API模型类:
class YoutubeAPIModel (val results: List<ApiModel>) {
}
ApiModel类别
data class ApiModel(
val kind: String,
val etag: String,
val nextPageToken: String,
val regionCode: String,
val pageInfo: PageInfo,
val items: List<Item>
)
项目类别
data class Item(
val etag: String,
val id: Id,
val kind: String,
val snippet: Snippet
)
代码段类
data class Snippet(
val channelId: String,
val channelTitle: String,
val description: String,
val liveBroadcastContent: String,
val publishTime: String,
val publishedAt: String,
val thumbnails: Thumbnails,
val title: String
)
缩略图类
data class Thumbnails(
val default: Default,
val high: High,
val medium: Medium
)
默认类
data class Default(
val height: Int,
val url: String,
val width: Int
)
中级
data class Medium(
val height: Int,
val url: String,
val width: Int
)
高级
data class High(
val height: Int,
val url: String,
val width: Int
)
正文:
{
"kind": "youtube#searchListResponse",
"etag": "753UP2XJw3TXoRmm3zEGF55fGP0",
"nextPageToken": "CAUQAA",
"regionCode": "CA",
"pageInfo": {
"totalResults": 1000000,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "WppfAkVnlxM77-c4C5YkSMZy8tE",
"id": {
"kind": "youtube#video",
"videoId": "apsAw6pzK8k"
},
"snippet": {
"publishedAt": "2020-11-27T15:15:00Z",
"channelId": "UClkZ3VjIiEcW0iiQsI2VCzQ",
"title": "Justin reagiert auf Montes kranken Gamingroom.. | Reaktion",
"description": "PESO™: https://pesoclo.com/ (werbung) Twitch: http://twitch.tv/jstinLIVE PRO: http://youtube.com/jstin17 Instagram: http://instagram.com/jstin ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/apsAw6pzK8k/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/apsAw6pzK8k/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/apsAw6pzK8k/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "justinLITE",
"liveBroadcastContent": "none",
"publishTime": "2020-11-27T15:15:00Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "JA3RfxF6EBqBmmNRxsedtOKMcJs",
"id": {
"kind": "youtube#video",
"videoId": "MPbUaIZAaeA"
},
"snippet": {
"publishedAt": "2020-11-20T05:00:11Z",
"channelId": "UC4-TgOSMJHn-LtY4zCzbQhw",
"title": "Shawn Mendes, Justin Bieber - Monster",
"description": "Listen to “Monster” now: https://Monster.lnk.to/Single New album WONDER from Shawn Mendes available Dec 4th Pre-order now: https://wonder.lnk.to/Album ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/MPbUaIZAaeA/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/MPbUaIZAaeA/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/MPbUaIZAaeA/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "ShawnMendesVEVO",
"liveBroadcastContent": "none",
"publishTime": "2020-11-20T05:00:11Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "HxnPWxJ157hcm5oisVd5m8d6AuA",
"id": {
"kind": "youtube#channel",
"channelId": "UCIwFjwMjI0y7PDBVEO9-bkQ"
},
"snippet": {
"publishedAt": "2007-01-15T21:17:27Z",
"channelId": "UCIwFjwMjI0y7PDBVEO9-bkQ",
"title": "Justin Bieber",
"description": "Help change the world. http://monster.lnk.to/single.",
"thumbnails": {
"default": {
"url": "https://yt3.ggpht.com/ytc/AAUvwnjpa2md8Bfk-LdYllfDdWWdF6CpKebvAlI5NifS6Q=s88-c-k-c0xffffffff-no-rj-mo"
},
"medium": {
"url": "https://yt3.ggpht.com/ytc/AAUvwnjpa2md8Bfk-LdYllfDdWWdF6CpKebvAlI5NifS6Q=s240-c-k-c0xffffffff-no-rj-mo"
},
"high": {
"url": "https://yt3.ggpht.com/ytc/AAUvwnjpa2md8Bfk-LdYllfDdWWdF6CpKebvAlI5NifS6Q=s800-c-k-c0xffffffff-no-rj-mo"
}
},
"channelTitle": "Justin Bieber",
"liveBroadcastContent": "upcoming",
"publishTime": "2007-01-15T21:17:27Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "W8Plz-NvW5Wu10V8eYId5iFo_QU",
"id": {
"kind": "youtube#video",
"videoId": "xQOO2xGQ1Pc"
},
"snippet": {
"publishedAt": "2020-10-16T04:00:08Z",
"channelId": "UCHkj014U2CQ2Nv0UZeYpE_A",
"title": "Justin Bieber & benny blanco - Lonely (Official Music Video)",
"description": "Lonely is out now: http://smarturl.it/imsolonely Starring Jacob Tremblay and directed by Jake Schreier If you're feeling Lonely or know someone who is struggling ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/xQOO2xGQ1Pc/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/xQOO2xGQ1Pc/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/xQOO2xGQ1Pc/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "JustinBieberVEVO",
"liveBroadcastContent": "none",
"publishTime": "2020-10-16T04:00:08Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "UBm6g6muHKP8ax-leC622ZCX-Sw",
"id": {
"kind": "youtube#video",
"videoId": "8EJ3zbKTWQ8"
},
"snippet": {
"publishedAt": "2020-01-04T17:00:11Z",
"channelId": "UCHkj014U2CQ2Nv0UZeYpE_A",
"title": "Justin Bieber - Yummy (Official Video)",
"description": "Changes Out Now: https://justinbieber.lnk.to/Changes Follow Justin: http://facebook.com/justinbieber http://twitter.com/justinbieber ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/8EJ3zbKTWQ8/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/8EJ3zbKTWQ8/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/8EJ3zbKTWQ8/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "JustinBieberVEVO",
"liveBroadcastContent": "none",
"publishTime": "2020-01-04T17:00:11Z"
}
}
]
}
1条答案
按热度按时间mrfwxfqh1#
在我的例子中,在
app/build.gradle
文件中,在buildTypes
部分下,我将shrinkResources
和minifyEnabled
键设置为true
。为了使gson库正常工作,我必须注解它们或将它们设置为false。当它们被设置为true
时,gson库中的fromJson
方法总是返回null。