ambiguous reference to overloaded definition, from

2019-05-10 18:35发布

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条回答
孤傲高冷的网名
2楼-- · 2019-05-10 19:32

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.

查看更多
登录 后发表回答