如何解析包含Scala中始终具有更改名称的字段的json?

时间:2018-12-13 15:24:07

标签: json scala playframework play-json

我目前从我想进行JSON解析的API中收到非常大的JSON响应。我的应用程序在Scala中使用play和。 为此,我使用的是案例类,稍后将使用隐式解析。但是现在我已经意识到在一个案例类中,我有一个始终在更改名称的字段。一个人怎么解析呢?

例如,我具有json响应的这一部分:

"customerInfo": {
 "number": "123456XXXXXX7891",
    "email": "randomname@gmail.com",
    "billingFirstName": "Random",
    "billingLastName": "Name"
  },

我将对此进行解析:

case class CustomerInfo (
number: String,
email: String,
billingFirstName: String,
billingLastName: String
)

但是然后我在json里面有一个不断变化的字段,看起来像这样:

    "Check": {
      "5c123456":
        "{random numbers inside here}"
    }

此字段“ 5c123456”始终在变化,并且永远不会使用相同的“名称”。 我将如何解析它?如果不是,我可以以某种方式忽略此案例类Check并仍然解析其余的json吗?不胜感激。

已编辑以添加其他信息

为了澄清,我想做的是能够解析如下内容:

val result = Json.fromJson[RootInterface](res.json).get

这是整个响应的示例:

    val response = : """{
  "per_page": 50,
  "current_page": 1,
  "next_page_url": "http:\/\/testapi.com\/api\/v3\/testing\/list?page=2",
  "prev_page_url": null,
  "from": 1,
  "to": 50,
  "data": [
    {
      "customerInfo": {
        "number": "1234",
        "email": "felix@ytesting.com",
        "billingFirstName": "Felix",
        "billingLastName": "Testing"
      },
      "updated_at": "2018-12-13 16:10:08",
      "created_at": "2018-12-13 14:06:54",
      "fx": {
        "merchant": {
          "originalAmount": 1234,
          "originalCurrency": "EUR",
          "convertedAmount": 1234,
          "convertedCurrency": "EUR"
        }
      },
      "raw": {
        "Check": {
          "5c1283dad": "{\"id\":\"1234-5678-4da1-a3ea-5aa0c2b8a3f3\",\"status\":\"TEST\",\"risk_score\":0,\"descriptions\":{},\"testRuleId\":null,\"triggeredExceptionRuleId\":null,\"reason\":null}"
        }
      },
      "acquirer": {
        "type": "TESTCARD"
      },
      "transaction": {
        "merchant": {
          "referenceNo": "12345",
          "status": "TEST",
          "operation": "TEST",
          "type": "AUTH",
          "message": "TESTING",
          "customData": "1234",
          "created_at": "2018-12-12 15:10:07",
          "transactionId": "12345"
        }
      },
      "refundable": false,
      "merchant": {
        "id": 1234,
        "name": "Felix Testing",
        "allowPartialRefund": false,
        "allowPartialCapture": false
      },
      "ipn": {
        "sent": true,
        "merchant": {
          "transactionId": "123456789",
          "referenceNo": "1234",
          "amount": 1234,
          "currency": "EUR",
          "date": 1544717407,
          "code": "42",
          "message": "TESTING",
          "operation": "TEST",
          "type": "AUTH",
          "status": "DECLINED",
          "customData": "12345",
          "paymentType": "TESTCARD",
          "authTransactionId": "123456",
          "descriptor": "Felix Testing",
          "token": "123456789token",
          "convertedAmount": 1234,
          "convertedCurrency": "EUR",
          "ipnType": "TESTIP",
          "_queueReadCountKey": 1
        }
      }
    }
  ]
}"""

我有以下案件案例

case class Acquirer(
  `type`: String
)

case class CustomerInfo (
  number: String,
  email: String,
  billingFirstName: String,
  billingLastName: String
                        )

case class Data (
    customerInfo: CustomerInfo,
    updated_at: String,
    created_at: String,
    fx: Fx,
    raw: Option[Raw],
    acquirer: Acquirer,
    transaction: transaction,
    refundable: Boolean,
    merchant: Merchant2,
    ipn: Ipn
  )

  case class transaction(
  merchant : Merchant1
                        )

case class Fx (
  merchant: Merchant
)

case class Ipn (
 sent: Boolean,
 merchant: Merchant3
 )

case class Merchant (
originalAmount: Int,
originalCurrency: String,
convertedAmount: Int,
convertedCurrency: String
)

case class Merchant1 (
 referenceNo: String,
 status: String,
 operation: String,
`type`: String,
message: String,
customData: String,
created_at: String,
transactionId: String
)

case class Merchant2 (
   id: Int,
   name: String,
   allowPartialRefund: Boolean,
   allowPartialCapture: Boolean
   )

case class Merchant3 (
   transactionId: String,
   referenceNo: String,
   amount: Int,
   currency: String,
   date: Int,
   code: String,
   message: String,
   operation: String,
   `type`: String,
   status: String,
   customData: String,
   paymentType: String,
   authTransactionId: String,
   descriptor: String,
   token: String,
   convertedAmount: Int,
   convertedCurrency: String,
   ipnType: String,
   _queueReadCountKey: Int
)
                 )

case class Raw(Check: Map[String, String])


case class RootInterface (
   per_page: Int,
   current_page: Int,
   next_page_url: String,
   prev_page_url: String,
   from: Int,
   to: Int,
   data: List[Data]
 )

这些隐式:

implicit val RootInterface: Format[RootInterface] = Json.format[RootInterface]
  implicit val Data: Format[Data] = Json.format[Data]
  implicit val CustomerInfo: Format[CustomerInfo] = Json.format[CustomerInfo]
  implicit val Fx: Format[Fx] = Json.format[Fx]
  implicit val Transaction: Format[transaction] = Json.format[transaction]
  implicit val Acquirer: Format[Acquirer] = Json.format[Acquirer]
  implicit val Merchant: Format[Merchant] = Json.format[Merchant]
  implicit val Merchant1: Format[Merchant1] = Json.format[Merchant1]
  implicit val Merchant2: Format[Merchant2] = Json.format[Merchant2]
  implicit val Merchant3: Format[Merchant3] = Json.format[Merchant3]
  implicit val Ipn: Format[Ipn] = Json.format[Ipn]
  implicit val jsonFormat: Format[Raw] = Json.format[Raw]

1 个答案:

答案 0 :(得分:4)

如注释中所述,动态字段使用Map处理。这是一个示例:

像这样定义case-class

import play.api.libs.json._

case class MyObject(Check: Map[String, String])

定义隐式格式:

object MyObject {
  implicit val jsonFormat: OFormat[MyObject] = Json.format[MyObject] 
}

使用validate从Json创建case-class

val json = Json.parse("""{ "Check": {
      "5c123456":
        "123239"
    }}""")

json.validate[MyObject] // > JsSuccess(MyObject(Map(5c123456 -> 123239)),)

这里是Scalafiddle要检查的地方:Scalafiddle

这里是相关文档:JSON-automated-mapping