Scala如何将JsonAST.JValue转换为类型?

Scala How to convert JsonAST.JValue to a type?

我有一个 angular 应用程序可以像这样向服务器发送请求:

$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 和 class

中使用相同的字段名称会更容易
case class Part(manufacturer: String, partNumber: String)
val part: Part = jvalue.extract[Part]

如果您无法更改您的案例中的字段名称class,您将需要按照此处所述实施自定义序列化程序