我有這樣的json數據。我想解析bidRequestString,bidResponseCreative,bidResponseData的嵌套對象。傑克遜解析json嵌套對象是一個JSON Stringify字符串
這是json。
{
"auctionId": "6c676664666b6621dr46c6b6c6c6a68",
"bidRequestString": "{\"id\":\"6c676664666b6621dr46c6b6c6c6a68\",\"timestamp\":\"2015-09-04T04:44:24.381Z\",\"isTest\":false,\"url\":\"http://www.bsd.vvv.com/\",\"userAgent\":\"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070118 Firefox/2.0.0.2pre\",\"protocolVersion\":\"Google Protocol Buffer\",\"exchange\":\"adx\",\"provider\":\"Google\",\"location\":{\"dma\":-1,\"metro\":-1,\"timezoneOffsetMinutes\":-1},\"segments\":{\"AdxDetectedVerticals\":[\"24:0.618452\",\"355:0.733486\",\"380:0.258120\",\"433:0.897398\",\"474:0.426707\"]},\"userIds\":{\"prov\":\"aGtqaq2ikdta21kZ2ZpbGZu70mo\",\"xchg\":\"aGtqaq2ikdta21kZ2ZpbGZu70mo\"},\"imp\":[{\"id\":\"66\",\"banner\":{\"w\":[220,300],\"h\":[600,450],\"id\":\"66\",\"pos\":0,\"topframe\":0},\"pmp\":{\"ext\":{\"adgroup_id\":\"17490739393\"}},\"formats\":[\"220x600\",\"300x450\"],\"position\":0},{\"id\":\"165\",\"banner\":{\"w\":120,\"h\":600,\"id\":\"165\",\"pos\":0,\"topframe\":0},\"pmp\":{\"ext\":{\"adgroup_id\":\"17490739393\"}},\"formats\":[\"120x600\"],\"position\":0}],\"spots\":[{\"id\":\"66\",\"banner\":{\"w\":[220,300],\"h\":[600,450],\"id\":\"66\",\"pos\":0,\"topframe\":0},\"pmp\":{\"ext\":{\"adgroup_id\":\"17490739393\"}},\"formats\":[\"220x600\",\"300x450\"],\"position\":0},{\"id\":\"165\",\"banner\":{\"w\":120,\"h\":600,\"id\":\"165\",\"pos\":0,\"topframe\":0},\"pmp\":{\"ext\":{\"adgroup_id\":\"17490739393\"}},\"formats\":[\"120x600\"],\"position\":0}],\"site\":{\"page\":\"http://www.bsd.vvv.com/\"},\"device\":{\"ua\":\"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070118 Firefox/2.0.0.2pre\",\"ip\":\"106.100.108.0\",\"geo\":{\"zip\":\" 3000\"},\"language\":\"en\",\"ext\":{\"geo_criteria_id\":1000567}},\"user\":{\"id\":\"aGtqaq2ikdta21kZ2ZpbGZu70mo\",\"ext\":{\"cookie_age_seconds\":1595306}}}",
"bidResponseCreative": "{\"itemId\":\"vkixi_cds0803_2015073133198\",\"campaignId\":\"859834ead1264843434cf6627ds43294d1\",\"htmlSnippet\":\"\\u003ciframe frameborder=0 scrolling=no width=\\\"300\\\" height=\\\"250\\\" src=\\\"//sv.xxx.com/adedge/api/bd/serving/simple/frame?aukey=1976ae04bc8eddsddsds34b02ae545333c\\u0026_=%%CACHEBUSTER%%\\u0026winning_price=%%WINNING_PRICE%%\\u0026google_click_url=%%CLICK_URL_ESC%%\\u0026encrypt_value=%%ENCRYPT_VALUE%%\\\"\\u003e\\u003c/iframe\\u003e\",\"name\":\"Expandable Web\",\"formatCode\":\"billboard\",\"bd\":\"\",\"status\":1,\"deleted\":false,\"landingPage\":\"https://zadds.sg\",\"tagging\":[1,2,3],\"expandingDirection\":14,\"bdUrl\":\"\",\"format\":{\"code\":\"billboard\",\"name\":\"Billboard\",\"publisher\":\"default\",\"type\":\"standard\",\"width\":120,\"height\":600,\"expanded_width\":0,\"expanded_height\":0,\"collapsed_width\":0,\"collapsed_height\":0,\"aspratio\":3.76,\"expandable\":true,\"expand_first\":true}}",
"bidResponseData": "{\"bids\":[{\"creative\":3,\"ext\":null,\"price\":\"72USD/1M\",\"priority\":1.0,\"spotIndex\":0},{\"creative\":2,\"ext\":null,\"price\":\"60USD/1M\",\"priority\":1.0,\"spotIndex\":1}]}",
"biddingMainAccount": "859834ead1264843434cf6627ds43294d1",
"impIndex": "1",
"impressionId": "165",
"rawWinPrice": "100USD/1M",
"timestamp": "2015-Sep-04 04:44:24.49340",
"userIds": "{\"prov\":\"aGtqaq2ikdta21kZ2ZpbGZu70mo\"}",
}
我做這樣的事情
case class Bids(creative: Int, price: String, spotIndex: Int)
case class BidResponseData(bids: Array[Bids])
//
case class UserId(prov: String, xchg: String)
case class BidRequestString(id: String, isTest: Option[Boolean],
url: String, userAgent: Option[String],
exchange: Option[String], provider: Option[String],
userIds: Option[UserId])
//
case class Format(width: Int, height: Int)
case class BidResponseCreative(format: Format)
//
case class WinMessage(auctionId: String, bidRequestString: BidRequestString,
bidResponseCreative: BidResponseCreative, bidResponseData: BidResponseData,
biddingMainAccount: String, impIndex: String,
rawWinPrice: String, timestamp: String)
val jsonSerializer = {
val m = new ObjectMapper() with ScalaObjectMapper
m.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
m.registerModule(DefaultScalaModule)
m
}
var message = jsonSerializer.readValue[WinMessage](eventMessage)
但我得到的錯誤:
ERROR Executor: Exception in task 0.0 in stage 2.0 (TID 2)
com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.knx.adx.storage.processor.BidRequestString]
所以我做這樣的事情每個元素,並將其解析成JSON對象。
var message = jsonSerializer.readValue[Map[Any, Any]](eventMessage)
message.foreach {
case (k, v) => {
if (k == "bidRequestString") {
val tempObj = jsonSerializer.readValue[BidRequestString](v.toString)
}
}
}
我想問的是,正確的方法來做到這一點?我能做些什麼來使其更好或更快解析?現在它只能解析大約2k-3k個記錄/秒。
您是如何生成這些Json對象的?他們是無效的 – Dici
@Dici JSON來自Ad Exchange系統,所以我需要處理這個問題。 – giaosudau
嘗試谷歌Gson解析器,它自動處理這個問題&&比傑克遜更好。 https://github.com/google/gson –