为什么Angular JSON作为ListBuffer进入? (以及如何解析...)

时间:2014-04-28 19:35:59

标签: json angularjs scala parsing playframework-2.0

编辑我最初描述错误 - 在下面重写。

出于某种原因,我无法让Angular以一种被解释为简单字符串的方式发送JSON有效负载。查看JsObject正文,由于某种未知原因,它总是ListBuffer

代码

Application.scala

(摘自here

  implicit val userReads: Reads[User] = (
    (JsPath \ "name").read[String] and
      (JsPath \ "language").read[Int]
    )((name: String, languageId: Int) => User(None, name, languageId))

  def submitUser = Action(BodyParsers.parse.json) {
    implicit request =>
      val userResult = request.body.validate[User]
      userResult.fold(
        errors => {
          BadRequest(Json.obj("status" -> "KO", "message" -> JsError.toFlatJson(errors)))
        },
        user => {
          userDao.add(user)
          Ok(Json.obj("status" -> "OK", "message" -> ("User '" + user.full_name + "' saved.")))
        }
      )
  }

User.scala

case class User(id: Option[Int], full_name: String, language: Int)

app.js

(仅限相关部分)

$scope.submit = function(event) {
    $scope.loadingTracker = promiseTracker();
    return $http.post('/user', {
        data: {name: 'Joe Blow', language: 1}
    }, { tracker: $scope.loadingTracker })
      .success(function(id) {
          console.log("Success! Created user with id " + id);
      });
}

(我尝试过使用JSON.stringify等)

异常

对此进行传统的“读取”会给我带来这个错误或类似的错误:

play.api.Application$$anon$1: Execution exception[[JsResultException: JsResultException(errors:List((,List(ValidationError(error.expected.jsstring,WrappedArray())))))]]
    at play.api.Application$class.handleError(Application.scala:293) ~[play_2.10-2.2.2.jar:2.2.2]
    at play.api.DefaultApplication.handleError(Application.scala:399) [play_2.10-2.2.2.jar:2.2.2]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10-2.2.2.jar:2.2.2]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10-2.2.2.jar:2.2.2]
    at scala.Option.map(Option.scala:145) [scala-library-2.10.4.jar:na]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2.applyOrElse(PlayDefaultUpstreamHandler.scala:261) [play_2.10-2.2.2.jar:2.2.2]
Caused by: play.api.libs.json.JsResultException: JsResultException(errors:List((,List(ValidationError(error.expected.jsstring,WrappedArray())))))
    at play.api.libs.json.JsValue$$anonfun$2.apply(JsValue.scala:64) ~[play-json_2.10-2.2.2.jar:2.2.2]
    at play.api.libs.json.JsValue$$anonfun$2.apply(JsValue.scala:64) ~[play-json_2.10-2.2.2.jar:2.2.2]
    at play.api.libs.json.JsResult$class.fold(JsResult.scala:71) ~[play-json_2.10-2.2.2.jar:2.2.2]
    at play.api.libs.json.JsError.fold(JsResult.scala:10) ~[play-json_2.10-2.2.2.jar:2.2.2]
    at play.api.libs.json.JsValue$class.as(JsValue.scala:62) ~[play-json_2.10-2.2.2.jar:2.2.2]
    at play.api.libs.json.JsObject.as(JsValue.scala:164) ~[play-json_2.10-2.2.2.jar:2.2.2]

因为数据如下所示:

> body = {play.api.libs.json.JsObject@14793} {"data":{"name":"Joe Blow","language":1}}和“JsObject”有一个成员fields,它是一个ListBuffer(在本例中,size = 1)

我知道这是一个完全“noob”的问题,但是我不知道如何从正文中读取ListBuffer,更不用说将其解析为案例类了。我想以最恰当的方式解析这个问题;对我来说,为什么这个简单的JSON被Play转变为ListBuffer是没有意义的。

有任何建议/想法吗?

1 个答案:

答案 0 :(得分:1)

我不确定您是否在正确的位置搜索数据。

(JsPath \ "name")

让我觉得你正在跳过"数据" JSON数据结构中的级别。您可以尝试添加

(JsPath \ "data" \ "name")

看看情况是否好转。此外,您可以尝试以这种方式定义直接定义隐式格式的Read:

 Json.format[User]

然后在你正在做的事情中阅读你的数据:

val placeResult = request.body.validate[User]

这些只是一些提示,我不是JSON也不是scala专家,我还没有尝试过代码......