Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing JSON and iterating through the object in Scala

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.

like image 410
José P. Airosa Avatar asked Dec 02 '22 01:12

José P. Airosa


1 Answers

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

like image 176
Tanjona Avatar answered Dec 04 '22 05:12

Tanjona