gpt4 book ai didi

json - Play 2.2.2 (Scala) Json parse as List[Class] 问题

转载 作者:行者123 更新时间:2023-12-04 19:51:34 24 4
gpt4 key购买 nike

给定以下 Json 数组:

{
"success": true,
"data": [
{
"id": 594,
"stage_id": 15,
"title": "test deal",
"value": 0,
"currency": "EUR",
"add_time": "2014-03-18 17:45:51",
"update_time": "2014-03-24 13:30:27",
"stage_change_time": "2014-03-24 13:30:27",
"active": true,
"deleted": false,
"status": "open",
"expected_close_date": null,
"stage_order_nr": 1,
"person_name": "test"
},
{
"id": 601,
"stage_id": 15,
"title": "test deal2 deal",
"value": 0,
"currency": "EUR",
"add_time": "2014-03-24 14:11:00",
"update_time": "2014-03-24 14:11:00",
"stage_change_time": "2014-03-24 14:11:00",
"active": true,
"deleted": false,
"status": "open",
"expected_close_date": null,
"stage_order_nr": 1,
"person_name": "test deal2"
}
],
"additional_data": {
"pagination": {
"start": 0,
"limit": 100,
"more_items_in_collection": false
}
}
}

我想从中得到一个交易列表,我正在尝试这样做

case class Deal(id: Long, stage_id: Long)

def getAllDeals(): List [Deal] = {

var holder : WSRequestHolder = WS.url(PipeDriveApiBaseUrl + "/deals")

val complexHolder: WSRequestHolder = holder.withQueryString("filter_id" -> "9", "api_token" -> SalesManagerApiKey)
val futureResponse: Future[Response] = complexHolder.get()

implicit val dealReader = Json.reads[List[Deal]]

val futureJson: Future[List[Deal]] = futureResponse.map(
response => (response.json \ "data").validate[List[Deal]].get
)

我得到与隐式读取值相关的异常No unapply function found。但是注释掉它,我会得到 No Json deserializer found for type List[models.Deal]。尝试为此类型实现隐式读取或格式。

我无法用这些答案解决问题 herehere .我错过或误解了什么?

最佳答案

不是为 List[Deal] 定义一个隐式的 Json.reads,而是为 Deal 创建一个:

implicit val dealReader = Json.reads[Deal]

Play 已经内置了一个用于列表的隐式 JSON 读取转换器。实际上,它有一个用于所有 Traversables。你可以look at the code , 但它有点难读。问题是:Play 可以将 JSON 列表转换为 List 对象。它不知道如何读取/转换 Deal,这就是为什么您需要上面提到的隐式定义。

关于json - Play 2.2.2 (Scala) Json parse as List[Class] 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22634750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com