我想将JsonPath的这个示例转换为Scala。使用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
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句