我有一个角度应用程序将请求发送到服务器,如下所示:
$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))))))
有没有办法将其转换为List [Part]?
case class Part(mfr: String, pn: String)
答案 0 :(得分:1)
如果在json和case类
中使用相同的字段名称会更容易case class Part(manufacturer: String, partNumber: String)
val part: Part = jvalue.extract[Part]
如果您无法更改案例类中的字段名称,则需要按照此处的说明实现自定义序列化程序Deserialization of case object in Scala with JSON4S