2

How do I get the values of access Token if the Json String returned is this:

var result = Http("https://example.com").postForm(Seq("clientId" -> clientId, "clientSecret" -> clientSecret)).asString

var jsonObj = scala.util.parsing.json.JSON.parseFull(result.
accessToken = jsonObj.get("accessToken")

The Result of the request to example.com is:

{
    "accessToken": "xxyyyzzz",
    "expiresIn": 3600
}
Jimmy
  • 31
  • 1
  • 6
  • Are you having some problem... I think this code should be working fine... considering that you change last line because `scala.util.parsing.json.JSON.parseFull( result )` returns `Option[ Any ]` – sarveshseri Feb 23 '15 at 05:05
  • Im getting the compilation error: Any does not take parameters – Jimmy Feb 23 '15 at 05:07

1 Answers1

3

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 _ => ""
    }
}
sarveshseri
  • 13,738
  • 28
  • 47
  • I am now getting the error: "missing parameter type for expanded function [error] The argument types of an anonymous function must be fully known. (SLS 8.5) [error] Expected type was: ? [error] case Some( mapAsAny) => {" – Jimmy Feb 23 '15 at 19:07
  • Ohh... I did a mistake in the code. Fixed it now. Change this - `case Some( mapAsAny ) => {` to `case Some( mapAsAny ) => mapAsAny match {`. – sarveshseri Feb 23 '15 at 19:35
  • While this is an excellent answer if you control the server code then you should consider using something like Argonaut which looks at your Scala case classes and automatically converts both to and from json. See this answer where it seamlessly deals with structured json/objects using just two lines of code to ask it to create the mapping http://stackoverflow.com/a/18675897/329496 – simbo1905 Feb 23 '15 at 19:47
  • Though Argonaut is great... I will recommend other libraries more ( just becasue of better documentation and tutorials ) such as - Gson, Jackson or PlayJson. – sarveshseri Feb 23 '15 at 20:03