Swapnil Swapnil - 1 year ago 190
Scala Question

scala iterate over JSON Array

I have a very simple JSON as below and I want to get

sJson[0], sJson[1]
objects, How can I achieve this ?

scala> val jsonA = "[{'foo': 4, 'bar': 'baz'},{'bla': 4, 'bar': 'bla'}]"
jsonA: String = [{'foo': 4, 'bar': 'baz'},{'bla': 4, 'bar': 'bla'}]

scala> val sJson = parseFull(jsonA)
sJson: Option[Any] = None

scala> println(sJson.toString())

enter image description here

Answer Source

JSON notation wants you to surround variables with double quotes:

val jsonA = """[{"foo": 4, "bar": "baz"},{"bla": 4, "bar": "bla"}]"""
val sJson = parseFull(jsonA).get // List(Map(foo -> 4.0, bar -> baz), Map(bla -> 4.0, bar -> bla))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download