JSON to map [String, JsObject] to Play 2.0?

I'm new to Play! and Scala, but I'm trying to make a service that will display a JSON request on a map [String, JsObject] (or Map [String, JsValue], I'm not sure about the difference), and then output the list of keys recursively through the map (preferably like a tree) .

But I have problems starting up:

def genericJSONResponse = Action(parse.json) {
  request => request.body
  var keys = request.keys
  Ok("OK")
}

What I would expect here is that the keys are filled with request keys, but of course it does not compile. How do I approach this, given the description above?

Thanks in advance for helping Scala noob :-)

Nick

+5
source share
1 answer

JsValue - JSON. JsObject JsValue ( JsNull, JsUndefined, JsBoolean, JsNumber, JsString JsArray). JSON, : http://json.org/

, JSON body JSON ( , ), :

def genericJSONResponse = Action(parse.json) { request =>
  request.body match {
    case JsObject(fields) => Ok("received object:" + fields.toMap + '\n')
    case _ => Ok("received something else: " + request.body + '\n')
  }
}

fields.toMap , : Map[(String, JsValue)], map collect . (, fields, a Seq[(String, JsValue)] map collect).

+14

All Articles