scala - Play 2.1 Reading JSON Objects in order -
json parse: http://www.dota2.com/jsfeed/heropickerdata?v=18874723138974056&l=english
hero class , json serialization
case class hero( var id:option[int], name: string, bio: string, var truename:option[string] ){} implicit val modelreader: reads[hero] = json.reads[hero]
reading data
val future: future[play.api.libs.ws.response] = ws.url("http://www.dota2.com/jsfeed/heropickerdata?v=18874723138974056&l=english").get() val json = json.parse(await.result(future,5 seconds).body).as[map[string, hero]] var = 1 json.foreach(p => { p._2.truename = some(p._1) p._2.id = some(i) p._2.committodatabase += 1 })
i need id of each hero. order of heros in json matches id. map unordered , wont work. have other ideas?
i have tried use linkedhashmap. tried make implicit reads linkedhashmap i've failed. if thinks answer please give me guidance?
it keeps saying "no json deserializer found type scala.collection.mutable.linkedhashmap[string,models.hero]. try implement implicit reads or format type.". have trait imported file i'm trying read from. have funny feeling last line in reads problem. think can't asinstanceof, have no other ideas of how reads.
linkedhashmap implicit reads code: http://pastebin.com/cf5npscx
you can try extracting data in order jsobject
returned json.parse
directly, possibly this:
val json = json.parse(await.result(future,5 seconds).body) val heroes: map[string, hero] = json match { case obj: jsobject => obj.fields.zipwithindex.map{ case ((name: string, herojson: jsvalue), id) => herojson.asopt[hero].map{ _.copy(id = some(id)) } }.flatten.tomap case _ = > seq.empty }
i don't believe you'll need order-preserving map anymore since ids generated , fixed.
Comments
Post a Comment