3

我正在试图将JsonPath的this example转换为Scala。它应该很容易与Java类似:对Java库重载定义的模糊引用

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

这我转换到这个斯卡拉:

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版本)。

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

+0

看起来像* JsonPath *库中的一个问题,因为第一个参数中的字符串总是不明确的。 – MKaama

回答

3

对于这个特定的情况下,你可能只是做

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

这将只匹配的重载方法之一(其中一个需要Object)。推测该方法调用json.toString这是微不足道的,因为我们已经有String

+0

有趣的是,我曾尝试asInstanceOf [String]和asInstanceOf [java.lang.String]没有任何运气。 – user833970

+0

我试过这个改变。虽然编译错误消失了,但是运行代码导致在json中找不到键的错误: 在路径$:com.jayway.jsonpath.PathNotFoundException com.jayway.jsonpath中未找到属性['s3Bucket']。 PathNotFoundException:Property ['s3Bucket'] not found in path $ at com.jayway.jsonpath.internal.token.PropertyPathToken.evaluate(PropertyPathToken.java:41)........ –