Я пытаюсь использовать Retrofit 2 для анализа JSON.Я должен сделать вызов карты для моего проекта.Вот мой код:
retrofit2.Call<Map<String, Channel>> call =restInterface.getChannels();
call.enqueue(new Callback<Map<String, Channel>>() {
@Override
public void onResponse(Response<Map<String, Channel>> response) {
Map<String, Channel> body = response.body();
for (Channel channel : body.values()) {
System.out.println(channel.getSong());
}
}
@Override
public void onFailure(Throwable t) {
// TODO
}
});
Я получаю ошибку несовместимого типа, и Android Studio предлагает обратиться сюда:
retrofit2.Call<Map<String, Channel>> call =restInterface.getChannels();
к этому:
retrofit2.Call<List<Channel>> call =restInterface.getChannels();
Я знаюэто потому, что оригинальный тип Retrofit, но для моего проекта я должен сделать этот вызов.Здесь вы можете увидеть, как мой JSON выглядит в API:
{
"channel0":{
"song":"Cry Cry",
"artist":"Oceana",
"duration":"180",
"playedat":"1545158211",
"image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/550513f3aaa5b80648972e715af345e5.png"
},
"channel1":{
"song":"Phantom Of The Opera",
"artist":"Iron Maiden",
"duration":"420",
"playedat":"1545158002",
"image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/b2cf018fb80147428b698452b2512996.png"
},
"channel2":{
"song":"Carmen Cubana",
"artist":"Klazz Brothers",
"duration":"180",
"playedat":"1545158161",
"image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/d518730378364a72ab98460bd0fe3d1a.png"
},
"channel3":{
"song":"Shut Up And Let Me Go",
"artist":"The Ting Tings",
"duration":"120",
"playedat":"1545158234",
"image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/c5805b786a3e4f19b747c7a3dbb41d15.png"
},
"channel4":{
"song":"The Racing Rats",
"artist":"Editors",
"duration":"240",
"playedat":"1545158095",
"image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/62544562d1904884b6e47804cf1ede1d.png"
}
}
Итак, как я могу это сделать?Я не знаю много об этой теме, поэтому я полностью открыт для предложений или примеров для моей проблемы.Спасибо за помощь.