Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a field in Scala json4s

Tags:

scala

json4s

How do I get a particular field out of a Json object in Scala? I feel like I'm going in circles.

import org.json4s._
import org.json4s.jackson.JsonMethods._

val me = parse(""" {"name":"brian", "state":"frustrated"} """)

Now I want just the state. I was looking for something like

me("state") -> "frustrated"

I have tried

me("state")
me.get("state")
me \ "state" <thanks for the idea>
me['state']
me.state
me.NOOOOOOOOOO!!!!!!!

Help?

like image 524
Brian Dolan Avatar asked Dec 19 '14 00:12

Brian Dolan


1 Answers

I think your code has errta, and below may be right code.

Assume type of value in state field is fixed, say its type is string.

val me = parse("""{"name":"brian", "state":"frustrated"}""")
val JString(state) = me \ "state"
like image 104
Injun Song Avatar answered Oct 14 '22 09:10

Injun Song