简体   繁体   English

如何在Play Scala中正确验证和转换JSON?

[英]How to properly validate and transform a json in play scala?

I am trying to parse and transform a json file so that it can fit a DB structure. 我正在尝试解析和转换json文件,使其适合数据库结构。 I have the following json file. 我有以下json文件。

{
  "request": {
    "Target": "AR",
    "Format": "jsonp",
    "Service": "HasOff",
    "Version": "2",
    "NetworkId": "xyz",
    "Method": "getConversions",
    "api_key": "xyz",
    "fields": [
      "Offer.name",
      "Stat.datetime",
      "Stat.conversion_status",
      "Stat.approved_payout",
      "Stat.ip",
      "Stat.ad_id",
      "Stat.affiliate_info1",
      "Stat.offer_id"
    ],
    "data_start": "2015-04-16 23:59:59",
    "data_end": "2015-04-18 00:00:00",
    "callback": "angular.callbacks._3",
    "_ga": "GA1.2.1142892596.1429262595",
    "_gat": "1"
  },
  "response": {
    "status": 1,
    "httpStatus": 200,
    "data": {
      "page": 1,
      "current": 100,
      "count": 521,
      "pageCount": 6,
      "data": [
        {
          "Offer": {
            "name": "ABC Company"
          },
          "Stat": {
            "datetime": "2015-04-19 12:09:01",
            "conversion_status": "approved",
            "approved_payout": "26.94000",
            "ip": "111.11.11.1",
            "ad_id": "123456",
            "affiliate_info1": "123456",
            "offer_id": "260"
          }
        },
        {
          "Offer": {
            "name": "ABC Company"
          },
          "Stat": {
            "datetime": "2015-04-11 01:01:38",
            "conversion_status": "approved",
            "approved_payout": "44.94000",
            "ip": "49.204.222.117",
            "ad_id": "123456",
            "affiliate_info1": "123456",
            "offer_id": "260"
          }
        },

To process it I wrote a case class, model the class and try to convert the json into the model as given in -- https://www.playframework.com/documentation/2.2.x/ScalaJson . 为了处理它,我写了一个case类,模型类,并尝试将JSON转换成模型所给出- https://www.playframework.com/documentation/2.2.x/ScalaJson The code snippet is given below. 代码段如下所示。 I have reduced the number of parameters in the case class for sake of brevity. 为了简洁起见,我减少了案例类中的参数数量。

case class ConversionReport(offerName: String, date: DateTime)
implicit val readConversion: Reads[ConversionReport] = (
      (__ \ "response" \ "data" \ "data" \ "Offer" \ "name").read[String] and
      (__ \ "response" \ "data" \ "data" \ "Stat" \ "datetime").read[DateTime]
  ) (ConversionReport)

  def getConversionReport(){

    val conversionUrl: String = "http://some_link" 
    val holder = WS.url(conversionUrl).get()
    val result = Await.result(holder, Duration(10, "second")).json
    val conversionResult: JsResult[ConversionReport] = result.validate[ConversionReport]
    println ("xxxx The conversion result is " +conversionResult)

  }

When I execute the code, I am getting the error -- 当我执行代码时,出现错误-

xxxx The conversion result is JsError(List((/offerName,List(ValidationError(error.path.missing,WrappedArray()))), (/date,List(ValidationError(error.path.missing,WrappedArray())))))

I guess, I am not able to properly model the json file. 我想我无法正确建模json文件。 Any help is appreciated. 任何帮助表示赞赏。

In this case data is an array, so your approach will not work. 在这种情况下,数据是一个数组,因此您的方法将行不通。 You will have to change few things. 您将不得不改变一些事情。

import play.api.libs.json._
import play.api.libs.json.Reads._

case class ConversionReport(offerName: String, date: DateTime)

implicit val readConversion: Reads[ConversionReport] = (
  (__\ "Offer" \ "name").read[String] and
  (__ \ "Stat" \ "datetime").read[DateTime]
) (ConversionReport)

case class FullReport( reportList: Seq[ ConversionReport ] )
implicit val readFullReport: Reads[ FullReport ] = (
  (__ \ "response" \ "data" \ "data").lazyRead( Reads.seq[ ConversionReport ]( readConversion ) )
) ( FullReport )

def getConversionReport(){

  val conversionUrl: String = "http://some_link" 
  val holder = WS.url(conversionUrl).get()
  val result = Await.result(holder, Duration(10, "second")).json

  val conversionResult: JsResult[FullReport] = result.validate[FullReport]
  println ("xxxx The conversion result is " +conversionResult)

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM