android - Representing Abstract JSON Objects as models in Java -
ok making api requests retrieve things movies, songs, or ping server. of these responses contained within same response json object has varying fields depending on response. below 3 examples.
ping
{ "response" : { "status" : "ok", "version" : "0.9.1" } } getindexes
{ "response" : { "status" : "ok", "version" : "0.9.1", "indexes" : { "index" : [ { "name" : "a", "movie" : [ { "id" : "150", "name" : "a movie" }, { "id" : "2400", "name" : "another movie" } ] }, { "name" : "s", "movie" : [ { "id" : "439", "name" : "some movie" }, { "id" : "209", "name" : "some movie part 2" } ] } ] } } } getrandomsongs
{ "response" : { "status" : "ok" "version" : "0.9.1" "randomsongs" : { "song": [ { "id" : "72", "parent" : "58", "isdir" : false, "title" : "letter yokosuka", "album" : "metaphorical music", "artist" : "nujabes", "track" : 7, "year" : 2003, "genre" : "hip-hop", "coverart" : "58", "size" : 20407325, "contenttype" : "audio/flac", "suffix" : "flac", "transcodedcontenttype" : "audio/mpeg", "transcodedsuffix" : "mp3", "duration" : 190, "bitrate" : 858, "path" : "nujabes/metaphorical music/07 - letter yokosuka.flac", "isvideo" : false, "created" : "2015-06-06t01:18:05.000z", "albumid" : "2", "artistid" : "0", "type" : "music" }, { "id" : "3135", "parent" : "3109", "isdir" : false, "title" : "forty 1 mosquitoes flying in formation", "album" : "tame impala", "artist" : "tame impala", "track" : 4, "year" : 2008, "genre" : "rock", "coverart" : "3109", "size" : 10359844, "contenttype" : "audio/mpeg", "suffix" : "mp3", "duration" : 258, "bitrate" : 320, "path" : "tame impala/tame impala/04 - forty 1 mosquitoes flying in formation.mp3", "isvideo" : false, "created" : "2015-06-29t21:50:16.000z", "albumid" : "101", "artistid" : "30", "type" : "music" } ] } } } so question is, how should structure model classes use parsing these responses? @ moment have abstract response object contains fields status , version. however, using approach need response class extends abstract class ever request make (e.g. abstractresponse, indexesresponse, randomsongsresponse). also, models same name may have different fields depending on api request made. prefer avoid making model class every possible scenario.
and note, using gson json serialization/deserialization , retrofit communicate api.
Comments
Post a Comment