How to convert JSON to a type in Scala

Viewed 29682

My problem is I receive an JSON text from say, twitter. Then I want to convert this text to an native object in scala. Is there a standard method to do this? I'm also using Play 2

Here is what I have

import scala.io.Source.{fromInputStream}
import java.net._

val url = new URL("https://api.twitter.com/1/trends/1.json")
val content = fromInputStream( url.openStream ).getLines.mkString("\n")
val json = Json.parse(content)
val a = (json \ "trends" )
Ok(a(0))

I want to get all the trends name from the JSON

5 Answers

convert to jsvalue by using Json.parse(string) then add operator as[T] to extract the value

Related