Basically, scala.util.parsing.json.JSON.parseFull
returns Option[ Any ]
.
Any
because the return type depends on the structure of the JSON input.
Option
because your JSON string can be erroneous and hence None
in case of error and Some[ Any ]
in case of success.
So... In this case your JSON is,
{
"accessToken": "xxyyyzzz",
"expiresIn": 3600
}
Which is clearly a Map - type thing. So... In this case the return type will be an instance of Option[ Map[ String, Any] ]
but will be refereed to by a variable of type Option[ Any ]
.
So... What you have to do is following,
val optionAny = scala.util.parsing.json.JSON.parseFull( result )
val accessToken = optionAny match {
case None => ""
case Some( mapAsAny ) => mapAsAny match {
case m: Map[ String, Any ] => {
// Map[ A, B].get( key: A ) returns Option[ B ]
// So here m.get( "accessToken" ) Will return Option[ Any ]
val optionToken = m.get( "accessToken" )
optionToken match {
case None => ""
case Some( strAsAny ) => strAsAny match {
case str: String => str
case _ => ""
}
}
}
case _ => ""
}
}