How to replace a JSON value in Play

后端 未结 5 1907
遥遥无期
遥遥无期 2020-12-31 01:58

How do I replace a value in a JSON value in Play?
Code to illustrate:

def newReport() = Action(parse.json) { request =>
    var json = request.bod         


        
5条回答
  •  情歌与酒
    2020-12-31 02:48

    a glorified version of Zeimyth's answer that uses implicit conversion

    implicit class JsObjectEnhancer(jsObject: JsObject) {
      def update(args: (String, Json.JsValueWrapper)*): JsObject = {
        jsObject ++ Json.obj(args: _*)
      }
    }
    

    usage (json must be of type JsObject and the implicit class should be in scope)

    json.update("customerId", 1000)
    

提交回复
热议问题