JSON parsing and iteration through an object in Scala

Given, for example, the following JSON string:

[{"id": "user1", "password": "ps1"},{"id": "user2", "password": "ps2"},{"id": "user3", "password": "ps3"}] 

What is the best and most optimized way to analyze it in Scala and repeat each result and analyze it correctly?

Thanks.

+4
source share
5 answers

I think this blogpost gives an exhaustive answer to your question: http://debasishg.blogspot.com/2011/02/applicatives-for-composable-json.html at the end there is also a link to the full source repository.

+6
source

using 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 explained here: http://www.assembla.com/spaces/liftweb/wiki/JSON_Support

+8
source
+2
source

In addition to the lift-json approach and the type approach mentioned above, I know about spray-json (PEG parser) and twitter json lib (based on the code in the Program in the Scala book) and json lib in blue tones. There are others!

I suggest taking a look at Jackson , which is a mature and feature rich library for JSON analysis with Java.

Jackson has an "official" extension for scala: jackson-module-scala , and another Jerkson .

+1
source

There is a JSON parsing library in the structure, built using parser combinators: http://www.scala-lang.org/api/current/scala/util/parsing/json/package.html

Odersky and Venners will guide you through in their book , one of the last chapters.

+1
source

All Articles