2015-04-23 68 views
0

我想解析和轉換一個json文件,以便它可以適合數據庫結構。我有以下json文件。如何正確地驗證和轉換播放scala中的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" 
      } 
     }, 

要處理它,我寫了一個case類,模型類,並嘗試將JSON轉換成模型所給出 - https://www.playframework.com/documentation/2.2.x/ScalaJson。代碼片段如下。爲了簡潔起見,我減少了case類中的參數數目。

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) 

    } 

當我執行的代碼,我得到的錯誤 -

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

我想,我不能JSON文件正確建模。 任何幫助表示讚賞。

+0

嗯......我想我們都需要Gogeta在這裏。 :) –

+0

重新思考....好....數據是一個數組...所以它不會像那樣工作。 –

+0

數據也是json中的兩倍。 –

回答

1

在這種情況下,數據是一個數組,所以你的方法將無法工作。你將不得不改變一些東西。

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) 

} 
+0

謝謝。這只是略有改變。隱式val readFullReport:讀取[FullReport] =( (__ \「response」\「data」\「data」)。lazyRead(Reads.seq [ConversionReport](readConversion)) )map FullReport.apply _ –

相關問題