Saturday, November 26, 2011

Using Lift JSON to return a Map from JSON

It's easy to convert a JSON string in to a key/value pair Map with Lift JSON. I found out how to do this from the Stack Overflow question Can I use the Scala lift-json library to parse a JSON into a Map?

First, if you don't already have Lift JSON in your project, you'll need to add it. For SBT projects, add this to your project build file:

Here is the example code for how to get a Map back:

That's all there is to it. If you knew that every value was a particular type, like String, you could specify that instead of Any in the asInstanceOf conversion.

No comments: