[英]converting from Java script Json to Scala Json without eleminating field name for update operation
I use a function to convert from a json information sent from the front end: 我使用一个函数将前端发送的json信息转换为:
def convertFromJson(json: JsValue): Item = {
Item(
(json \ "id").asOpt[String],
(json \ "name").asOpt[String],
(json \ "project").asOpt[String],
(json \ "price").asOpt[scala.math.BigDecimal],
if ((json \ "quantity").asOpt[String].contains("NaN"))
{
None
}
else
{
(json \ "quantity").asOpt[scala.math.BigDecimal]
}
)
}
If a normal number like 2.0 for the quantity field is entered then I wll be able to see a json object like: 如果在数量字段中输入正常数字(例如2.0),那么我将能够看到一个json对象,例如:
{"id":"5aa7367","quantity":2}
The problem is that if my front end sends a NaN value to the back end for the Quantity field then I get: 问题是,如果我的前端向“数量”字段的后端发送一个NaN值,那么我得到:
{"id":"5aa7367"}
Is there a way to have something like {"id":"5aa7367","quantity":None}. 是否可以使用类似{“ id”:“ 5aa7367”,“ quantity”:None}之类的方法。 Quantity has been declared as BigDecimal. 数量已声明为BigDecimal。
我想您是想办法解决了,但是这是我的看法-对于NaN情况, quantity
似乎返回为空,因此您应该简单地选择None
?
if ((json \ "quantity").asOpt[Int].isEmpty)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.