Getting a field in Scala json4s

How do I get a specific field from 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 only want a state. I was looking for something like

me("state") -> "frustrated"

      

I tried

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

      

reference

+3


source to share


1 answer


I think your code has errta while below may be correct code.

Let's assume that the type of the value in the status field is fixed, for example, its type is string.



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

      

+5


source







All Articles