我在play2.3中有这样的API - 反应性mongo -
def addEndUser = Action.async(parse.json) { request =>
val cursor: Cursor[JsObject] = collectionEndUser.find(Json.obj("mobileNumber" -> "9686563240","businessUserId" ->"1")).
sort(Json.obj("createDate" -> -1)).cursor[JsObject]
val futureEndUserList: Future[List[JsObject]] = cursor.collect[List]()
futureEndUserList.map { user =>
val x:JsObject = obj(Map("endUsers" -> toJson(user) ))
println(x)
}
request.body.validate[User].map { user =>
val jsonData = Json.obj(
"businessUserId" ->user.businessUserId,
"userId" -> user.userId,
"registrantId" ->user.registrantId,
"memberId" -> "",
"name" -> user.name,
"currentPoints" -> user.currentPoints,
"email" -> user.email,
"mobileNumber" -> user.mobileNumber,
"mobileCountryCode" ->user.mobileCountryCode,
"createDate" -> (new java.sql.Timestamp(new Date().getTime)).toString,
"updateDate" -> (new java.sql.Timestamp(new Date().getTime)).toString,
"purchasedAmtForRedemption"->user.purchasedAmtForRedemption
)
collectionEndUser.insert(jsonData).map { lastError =>
Logger.debug(s"Successfully inserted with LastError: $lastError")
Created
}
}.getOrElse(Future.successful(BadRequest("invalid json")))
}
def findEndUserByUserId(userId: String) = Action.async {
val cursor: Cursor[JsObject] = collectionEndUser.find(Json.obj("userId" -> userId)).
sort(Json.obj("createDate" -> -1)).cursor[JsObject]
val futureEndUserList: Future[List[JsObject]] = cursor.collect[List]()
//val futureEndUserJsonArray: Future[JsArray] = futureEndUserList.map { endUser =>
//Json.arr(endUser)
//}
futureEndUserList.map { user =>
Ok(toJson(Map("endUsers" -> toJson(user) )))
}
}
此API称为POST方法,用于在DB中存储这些字段。但是在添加数据库之前,我想从集合中获取一个值并在其中一个字段中使用它。尽管println(x)正在打印这样的对象 {“endUsers”:[{“_ id”:{“$ oid”:“543f6912903ec10f48673188”},“businessUserId”:“1”,“createDate”:“2014-10-16 12:13:30.771”,“currentPoints” :16.0, “电子邮件”: “ruthvickms@gmail.com”, “mobileCountryCode”: “+ 91”, “移动电话号码”: “9686563240”, “名”: “Ruthvick”, “purchasedAmtForRedemption”:50.0, “updateDate”: “2014-10-17 20:23:40.725”,“userId”:“5”},{“_ id”:{“$ oid”:“543f68c0903ec10f48673187”},“businessUserId”:“1”,“userId”: “4”, “名”: “Ruthvick”, “currentPoints”: “0”, “电子邮件”: “ruthvickms@gmail.com”, “移动电话号码”: “9686563240”, “mobileCountryCode”: “+ 91”,” createDate“:”2014-10-16 12:12:08.692“,”updateDate“:”2014-10-16 12:12:08.692“,”purchaseAmtForRedemption“:”0“},{”_ id“:{”$ OID “:” 543f689e903ec10f48673186 “},” businessUserId “:” 1" , “用户id”: “3”, “名称”: “Ruthvick”, “currentPoints”: “0”, “电子邮件”:“ruthvickms@gmail.com “,”mobileNumber“:”9686563240“,”mobileCountryCode“:”+ 91“,”createDate“:”2014-10-16 12:11:34.079“,”updateDate“:”2014-10-16 12:11: 34.079" , “purchasedAmtForRedemption”: “0”},{ “_ ID”:{ “$ OID”: “543f63ef903ec10f48673185”},“业务用户ID “:” 1" , “用户id”: “2”, “名”: “Ruthvick”, “currentPoints”: “0”, “电子邮件”: “ruthvickms@gmail.com”, “移动电话号码”: “9686563240” ,“mobileCountryCode”:“+ 91”,“createDate”:“2014-10-16 11:51:35.394”,“updateDate”:“2014-10-16 11:51:35.394”,“purchaseAmtForRedemption”:“0 “}]},像这样解析
x.endUsers[0].name is throwing error like
identifier expected but integer literal found.
println(x.endUsers[0].name)
请帮我解析一下......我是游戏框架的初学者。
由于
答案 0 :(得分:0)
我认为这就是你要找的东西:
(x \" endUsers")(0)\" name"
但你不需要写:val x:JsObject = obj(Map(" endUsers" - > toJson(user))),因为用户本身就是JsObject的List。因此用户(0)\"名称"也会这样做。
详细信息:https://www.playframework.com/documentation/2.3.x/ScalaJson