ambiguous reference to overloaded definition, from a Java library

╄→гoц情女王★ 提交于 2019-12-07 16:38:04

问题


I was tying to convert this example for JsonPath to Scala. It should be easy with java like:

List<String> authors = JsonPath.read(json, "$.store.book[*].author");

Which I converted to this Scala:

val authors = JsonPath.read(json, "$.store.book[*].author");

Where json is a String. But I get this compile error.

ambiguous reference to overloaded definition, both method read in object JsonPath of type [T](x$1: String, 
 x$2: String, x$3: <repeated...>[com.jayway.jsonpath.Filter[_]])T and method read in object JsonPath of type 
 [T](x$1: Any, x$2: String, x$3: <repeated...>[com.jayway.jsonpath.Filter[_]])T match argument types 
 (String,String)

Which I assume is related to

public static <T> T read(Object json, String jsonPath, Filter... filters) 

and

public static <T> T read(String json, String jsonPath, Filter... filters) 

from com.jayway.jsonpath.JsonPath (version 0.9.1).

How can I disambiguate this function call?


回答1:


For this specific case you could just do

JsonPath.read(json.asInstanceOf[Object], "$.store.book[*].author")

This will only match one of overloaded methods (the one which takes Object). Presumably the method calls json.toString which is trivial since we already have a String.



来源:https://stackoverflow.com/questions/23331551/ambiguous-reference-to-overloaded-definition-from-a-java-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!