これは初心者の質問かもしれませんが、私は本当にそれについて申し訳ありませんが、私はどこでもそれを見つけることができませんでした。記述されたコードと出力。Play Framework Scala:name:Noneのようなキー値のペアでヌル値を取ることは可能ですか?
Using Play Framework 2.3.9 (Scala)
// controller
object Products extends Controller {
def list = Action {
Ok(Json.obj("products" -> Product.all))
}
}
// Model
case class Product(id: Long, name: Option[String])
object Product {
implicit val format: Format[Product] = Json.format[Product]
def all = DB.withConnection { implicit c =>
SQL("SELECT * FROM Products;").apply().map { row =>
Product(
id = row[Long]("id"),
name = row[Option[String]]("name")
)
}.toList
}
}
output generated:
{
"products": [{
"id": 1,
"name": "test"
}, {
"id": 2
}]
}
あなたが探しているものoutput wanted:
{
"products": [{
"id": 1,
"name": "test"
}, {
"id": 2,
"name": null
}]
}
'{" name ":None}'は無効ですJSON。 '{" name ":" None "}'は有効で、 '{" name ":null}' –
@RhysBradburyありがとう、更新されました。 –