【发布时间】:2019-05-12 19:05:33
【问题描述】:
我正在尝试使用 Retrofit 2 为我的 Android 项目解析 JSON URL,但我有一个问题。在我的 URL 中,JSON 如下所示:
{
"channel0":{
"song":"Crush",
"artist":"Jennifer Paige",
"duration":"180",
"playedat":"1545065265",
"image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/8eecc92227fcbb09b43472f000df74e1.png"
},
"channel1":{
"song":"Reasons Why",
"artist":"Brand New Immortals",
"duration":"180",
"playedat":"1545065371",
"image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/c059afda95dd35354af26cf72e5deab4.png"
},
"channel2":{
"song":"Dance Me To The End Of Love",
"artist":"Leonard Cohen",
"duration":"300",
"playedat":"1545065181",
"image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/a368617dc7dc4716a9badb523ff6e7d4.png"
},
"channel3":{
"song":"4 Minutes",
"artist":"Madonna",
"duration":"180",
"playedat":"1545065300",
"image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/1dcefc6496be4155a00f919dcbb54f77.png"
},
"channel4":{
"song":"Mothers, Sisters, Daughters",
"artist":"Voxtrot",
"duration":"180",
"playedat":"1545065257",
"image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/5861b97231bd4ffe9218dfcacc27d68a.png"
}
}
当我使用 POJO 时,它会为每个通道创建多个类。那么处理这个 JSON URL 的正确方法是什么?
【问题讨论】:
-
在我看来,这是 API 的错误。他们应该返回一个通道数组,在通道对象上有一个 ID,并删除变量键名。有很多方法可以处理它,但重新格式化响应是 IMO 的“正确”方式。
-
本站的代码应该写出来。这使我们可以更轻松地帮助您进行调试。
-
请不要张贴文字截图。任何文本,包括代码、错误或 JSON,都需要作为文本,而不是屏幕截图。
-
@meagar 将其更改为文本。
标签: java android json retrofit2 pojo