问题
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