scala - 对重载定义的不明确引用,来自 Java 库

我想转换 this example用于 Scala 的 JsonPath。使用 java 应该很容易,例如:

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

我将其转换为这个 Scala:
val authors = JsonPath.read(json, "$.store.book[*].author");

其中 json 是一个字符串。但是我收到这个编译错误。
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)

我认为这与
public static <T> T read(Object json, String jsonPath, Filter... filters) 


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

来自 com.jayway.jsonpath.JsonPath(0.9.1 版)。

如何消除此函数调用的歧义?

最佳答案

对于这种特定情况,您可以这样做

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

这只会匹配重载方法之一(采用 Object 的方法)。大概是该方法调用了 json.toString这是微不足道的,因为我们已经有了 String .

https://stackoverflow.com/questions/23331551/

相关文章:

clojure - 为什么 Clojure rationals 优先使用 Java `BigInte

google-maps - 嵌入式谷歌地图与 API V3

r - 将 R 中两个帧中的变量与循环进行比较

sonarqube - 为什么 return 语句会增加复杂度?

nunit - Nunit 中整个运行的设置和拆卸?

eclipse - org.eclipse.xtext.resource.FileNotFoundO

smartsheet-api - 工作表数据的预期语义是什么?应该返回空值吗?

c++-cli - '^' 与 Cpp 中的类名的含义

sql-server - Visual Studio 旗舰版 2013 : Server Explo

asp.net - Breeze 查询错误,即使结果返回