Reputation: 355
I have a router related data which is in following format:-
List(Map(routerId -> testR1,
config ->
List(
Map(pin -> X12, color -> YELLOW),
Map(pin -> M20, color -> BLACK)
)
),
Map(routerId -> testR3,
config ->
List(
Map(pin -> M12, color -> YELLOW),
Map(pin -> X20, color -> BLACK),
Map(pin -> M11, color -> RED)
)
)
)
and I have a Router case class
case class Router(routerId: String, modelInfo: List[Map[String,String]])
I am trying to create a list of Router for which I tried cast using asInstanceOf somewhat like this:
val data = request.get("data").get.asInstanceOf[List[Map[String, List[Map[String, String]]]]]
But, I would prefer not to cast it if possible. Is there a better way to do this?
Edit: The request is actually the json request which looks like this:
{
"data": [
{
"routerId": "testR1",
"config": [
{
"pin": "X12",
"color": "Red"
},
{
"pin": "M15",
"color": "Yellow"
},
{
"pin": "X20",
"color": "Yellow"
}
]
},
{
"routerId": "testR2",
"config": [
{
"pin": "X20",
"color": "Black"
},
{
"pin": "M11",
"color": "Yellow"
}
]
}
]
}
I am using play 2 which is actually converting it to the List[Map...] format.
P.S. - I am a newbie to scala.
Upvotes: 1
Views: 427
Reputation: 4296
You can use play-json like this ..
import play.api.libs.json._
implicit val reads = Json.reads[Router]
val jsResult: JsResult[Router] = Json.fromJson[Router](jsonString)
println(jsResult.get)
for more see
https://www.playframework.com/documentation/2.5.x/ScalaJson https://www.playframework.com/documentation/2.5.x/ScalaJsonAutomated
Upvotes: 1