Given, for example, the following JSON string:
[{"id": "user1", "password": "ps1"},{"id": "user2", "password": "ps2"},{"id": "user3", "password": "ps3"}]
What's the best and most optimized way to parse it in Scala and iterate through every result and analise it properly?
Thank you.
I think this blogpost gives an comprehensive answer to your question: http://debasishg.blogspot.com/2011/02/applicatives-for-composable-json.html at the end there is also a link to a full source repository.
with Lift-JSON:
import net.liftweb.json.JsonParser._
import net.liftweb.json.DefaultFormats
val jsonString = //your jsonString....
case class Credential (id:String, password:String)
implicit val formats = DefaultFormats
val credentials = parse(jsonString).extract[List[Credential]]
credentials foreach { cred => println(cred.id + " " + cred.password) }
everything is explain here : http://www.assembla.com/spaces/liftweb/wiki/JSON_Support
You could use lift-json library : http://www.assembla.com/spaces/liftweb/wiki/JSON_Support
Aside from lift-json, and the type class approach mentioned above, I know of spray-json (PEG parser), and twitter's json lib (based on the code in the Programming in Scala book) and the json lib in blueeyes. There are others!
I suggest taking a look at Jackson which is a mature and feature-rich library for JSON-parsing from Java.
Jackson has an "official" extension for scala: jackson-module-scala and another one Jerkson.
There's a JSON parsing library in the framework, built using the parser combinators: http://www.scala-lang.org/api/current/scala/util/parsing/json/package.html
Odersky and Venners walk you through it in their book, one of the last chapters.