在Scala中对案例对象进行审理。
我有一个角度应用程序,该应用像这样向服务器发布请求:
$scope.downloadPartDetails = (parts, e) ->
req = {
method: 'POST',
url: '/downloads/partdetails',
headers: {
'Content-Type': "application/json; charset=utf-8"
},
data: { parts: [
{manufacturer: "mfr1", partNumber: "part num1"},
{manufacturer: "mfr2", partNumber: "part num2"},
{manufacturer: "mfr3", partNumber: "part num3"}
] }
}
$http(req)
它出现在这样的服务器上:
JArray(List(JObject(List(JField(manufacturer,JString(mfr1)), JField(partNumber,JString(part num1)))), JObject(List(JField(manufacturer,JString(mfr2)), JField(partNumber,JString(part num2)))), JObject(List(JField(manufacturer,JString(mfr3)), JField(partNumber,JString(part num3))))))
有没有办法将其转换为列表[部分]?
case class Part(mfr: String, pn: String)
如果您在JSON中使用相同的字段名称和Case class
case class Part(manufacturer: String, partNumber: String)
val part: Part = jvalue.extract[Part]
如果您无法更改案例类中的字段名称,则需要实现自定义序列化器,如此处所述,请使用JSON4S