I have a json list structure like this, how can we read this structure and convert in a list in scala, play framwork.
{
"0":{
"name":"Foo",
"age":10
},
"12":{
"name":"Bar",
"age":10
}
.
.
.
}
If, there were json structure like below, I could have done it easily, but since I am not that versed in parsing Json and actually new to scala too, I couldn't understand how to work with Json strucutures that have key as "0","12" like in the first example. I had been using Jerkson wrapper for Jackson for generating/parsing json/case-class.
{
{"name":"Foo","age":10},
{"name":"Bar",age:11}
}
Update:
I actually succeeded in parsing the json structure, in addition to Ryans answer,
import play.api.libs.json._
case class Doodad(name: String, age: Int)
object Doodad {
implicit val fmt = Json.format[Doodad]
def parserFuntion = {
var myJsValue: JsValue = Json.parse(jsonString)// added line in the ans
var requiredMap = myJsValue.validate[Map[String, Doodad]]
}
}
But now I have a new questions, the json structure I working with seems to be a bit irregular for eg:
{
"0":{
"name":"Foo",
"age":10
},
"12":{
"name":"Bar",
"age":10
},
"13":{
"name":"Foobar"
"age":20,
"meta":{
"desc":"Irregular Json",
"img":"Some Link"
}
},
.
.
.
}
Chances are some of the json list elements might have that extra "meta" key->value pair, and some might not. So, how can we deal with such situations. Any ideas??
case class Doodad(name: String, age: Int)
object Doodad {
implicit val fmt = Json.format[Doodad]
}
myJsValue.validate[Map[String, Doodad]] // JsResult[Map[String, Doodad]]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.