beatbrot
02/28/2018, 9:49 AMeric
02/28/2018, 9:53 AMeric
02/28/2018, 9:57 AMbeatbrot
02/28/2018, 9:57 AMcedric
02/28/2018, 9:55 PMdata class Track(val id: Long, val title: String)
data class TrackData(val data: List<Track>)
data class Album(val id: Int, val title: String, val link: String, val cover_big: String, val duration: Int,
val rating: Int, val tracks: TrackData)
fun t() {
val json =
URL("<http://api.deezer.com/album/53227232>").openConnection().inputStream.reader().readText()
Klaxon().parse<Album>(json)?.let {
println("Album: ${it.title} ${it.tracks.data.size} tracks")
}
}
Note that I adapted the data classes to match the shape of the JSON more closely. No need for an adapter.eric
03/01/2018, 1:37 AMcedric
03/01/2018, 2:41 AMparse
invocation)cedric
03/01/2018, 2:42 AMeric
03/01/2018, 6:35 AMBox<T>
.
which doesn't work in the KotlinJsonAdapter in Moshi right now.
without custom generic types, there's no need for anything special in Moshi.
from your code:
val adapter = Moshi.Builder().build().adapter<Album>(Album::class.java)
URL("<http://api.deezer.com/album/53227232>").openConnection().inputStream.use {
val album = adapter.fromJson(JsonReader.of(Okio.buffer(Okio.source(it))))
println(album)
}
works fine.cedric
03/01/2018, 6:36 AMAlbum::class.java
.cedric
03/01/2018, 6:44 AMBox
in https://pastebin.com/wTRRZn8Eeric
03/01/2018, 4:49 PM