如何使用Play framework将字符串作为长时间传入的JSON读取
我在编写从客户端传递到服务器的JSON读取时遇到问题。当ID应该是数字时,客户端总是将其作为字符串发送。当我尝试将它们读入Scala对象时,这会导致问题。我的case类希望它们能够与DB匹配,但我不确定如何将字符串读得那么长。我试着简单地在雇主id上使用.readNullable[Long],但它只返回了预期为jsnumber的验证错误 播放Scala 2.4.1 Scala 2.11.7如何使用Play framework将字符串作为长时间传入的JSON读取,json,scala,playframework,Json,Scala,Playframework,我在编写从客户端传递到服务器的JSON读取时遇到问题。当ID应该是数字时,客户端总是将其作为字符串发送。当我尝试将它们读入Scala对象时,这会导致问题。我的case类希望它们能够与DB匹配,但我不确定如何将字符串读得那么长。我试着简单地在雇主id上使用.readNullable[Long],但它只返回了预期为jsnumber的验证错误 播放Scala 2.4.1 Scala 2.11.7 implicit val person_reads: Reads[Person] = ( Read
implicit val person_reads: Reads[Person] = (
Reads.pure(-1L) and
Reads.pure(None) and
(JsPath \ "person" \ "given_name").readNullable[String](minLength[String](1)) and
(JsPath \ "person" \ "surname").readNullable[String](minLength[String](1)) and
(JsPath \ "person" \ "city").readNullable[String] and
(JsPath \ "person" \ "state").readNullable[String] and
(JsPath \ "person" \ "county").readNullable[String] and
(JsPath \ "person" \ "zip").readNullable[String] and
(JsPath \ "person" \ "country").readNullable[String] and
(JsPath \ "person" \ "email").readNullable[String](email) and
(JsPath \ "person" \ "phone").readNullable[String] and
(JsPath \ "person" \ "employer_id").readNullable[String] and
Reads.pure(Set[Long]()) and
Reads.pure("") and
Reads.pure("")
)(Person.apply _)
case class Person(
id:Long,
facebook_id:Option[Long],
given_name:Option[String],
surname:Option[String],
city:Option[String],
state:Option[String],
county:Option[String],
zip:Option[String],
country:Option[String],
email:Option[String],
phone:Option[String],
employer_id:Option[Long],
people_connection_ids:Set[Long],
added:String,
modified:String
)
示例JSON POST/PUT
{
"person": {
"id": 78447,
"facebook_id": 12345678987654321,
"given_name": "Jon",
"surname": "Smith",
"city": "",
"state": "",
"county": "",
"zip": "",
"country": "",
"email": "",
"phone": "",
"added": "",
"modified": "",
"employer_id": "1289592", <- This one gets passed as a string instead of number
"people_connection_ids": [
73
]
}
}
{
“人”:{
“id”:78447,
“facebook_id”:12345678987654321,
“给定名称”:“Jon”,
“姓氏”:“史密斯”,
“城市”:“,
“国家”:“,
“县”:“,
“zip”:“,
“国家”:“,
“电子邮件”:“,
“电话”:“,
“加上:”,
“已修改”:“已修改”,
“雇主id”:“1289592”,您可以将读取的字符串映射为数字
(__ \ "id").read[String].map[Long](_.toLong)
在@cchantep answer上扩展,在您的情况下
(JsPath \ "person" \ "employer_id").readNullable[String].map(_.map{_.toLong}) and
下面是使用play.api.libs.json
import play.api.libs.json._
val js = """
| {
| "person": {
| "id": 78447,
| "facebook_id": 12345678987654321,
| "given_name": "Jon",
| "surname": "Smith",
| "city": "",
| "state": "",
| "county": "",
| "zip": "",
| "country": "",
| "email": "",
| "phone": "",
| "added": "",
| "modified": "",
| "employer_id": "1289592",
| "people_connection_ids": [
| 73
| ]
| }
| }""".stripMargin
val json = Json.parse(js)
(json \ "person" \ "employer_id").asOpt[String].map(f => java.lang.Long.valueOf(f))
输出是
res0: Option[Long] = Some(1289592)
谢谢,我选择这个作为正确的答案,因为这是一个直接的答案。我最终在我的代码中使用了这个,(JsPath\“person”\“employer\u id”)。readNullable[String]。map[Option[Long]](safeStringOptToLong(\ux))而且
因为对我来说,它读起来更好,而且我已经在我的应用程序中构建并使用了safeStringToLong。谢谢你的帮助。我最终从你的答案中构建了我的答案,但我不得不改变部分,让它为我工作。因此,我选择了@k.c.sham的答案,因为它完全符合我的用例。