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
}
2
  • 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 ] Commented Feb 23, 2015 at 5:05
  • Im getting the compilation error: Any does not take parameters Commented Feb 23, 2015 at 5:07

1 Answer 1

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 _ => ""
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

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) => {"
Ohh... I did a mistake in the code. Fixed it now. Change this - case Some( mapAsAny ) => { to case Some( mapAsAny ) => mapAsAny match {.
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 stackoverflow.com/a/18675897/329496
Though Argonaut is great... I will recommend other libraries more ( just becasue of better documentation and tutorials ) such as - Gson, Jackson or PlayJson.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.