2016-11-18 32 views
3

我正在查詢spotify api的給定查詢的軌道列表使用ws,當我將JSON數據轉換爲的情況下,類我收到我還沒有搞清楚的錯誤...重載的方法值[讀取]不能應用於(String => SearchController.this.TrackSearch)

class SearchController @Inject() (
    val ws: WSClient 
    ) extends Controller { 

    case class TrackSearch(href: String) 

    implicit val trackResultsReads: Reads[TrackSearch] = (
     (__ \ "tracks" \ "href").read[String] 
    )(TrackSearch.apply _) 

    def index = Action.async { implicit request => 
    search("track", param(request, "q")).map { r => 
     val ts = r.json.as[TrackSearch] 
     println(ts) 
     Ok 
    } 
    } 

    private def search(category: String, query: String): Future[Try[WSResponse]] = { 
    ws.url("https://api.spotify.com/v1/search") 
     .withQueryString("q" -> query, "type" -> category) 
     .get() 
     .map(Success(_)) 
     .recover { case x => Failure(x) } 
    } 

    private def param(request: Request[AnyContent], name: String): String = { 
    request.queryString.get(name).flatMap(_.headOption).getOrElse("") 
    } 
} 

我得到的錯誤是:

Overloaded method value [read] cannot be applied to (String => SearchController.this.TrackSearch) 

    implicit val trackResultsReads: Reads[TrackSearch] 
> (__ \ "tracks" \ "href").read[String] 
)(TrackSearch.apply _) 

如果我在行動查詢JSPath,我可以將「href」字符串恢復正常,所以不是這樣的:

println(r._2.json \ "tracks" \ "href") 

回答

4

問題是隻有一個字段。如果你添加了第二個字段,它會編譯。我不完全明白爲什麼它不應該用一個字段來編譯。因此,在單場的情況下,嘗試以下操作:

implicit val trackResultsReads: Reads[TrackSearch] = { 
    ((__ \ "tracks" \ "href").read[String]) 
    .map(TrackSearch(_)) 
} 

Here是很舊的鏈接,我發現上面。使用不同的方法也可以參見this link獲得類似的SO問題。

相關問題