Java库中对重载定义的含糊不清的引用 [英] ambiguous reference to overloaded definition, from a Java library

查看:238
本文介绍了Java库中对重载定义的含糊不清的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将JsonPath的此示例转换为Scala.像这样的Java应该很容易:

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");

我将其转换为此Scala:

Which I converted to this Scala:

val authors = JsonPath.read(json, "$.store.book[*].author");

其中json是字符串.但是我遇到了这个编译错误.

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)

我认为与哪个有关

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).

from com.jayway.jsonpath.JsonPath (version 0.9.1).

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

How can I disambiguate this function call?

推荐答案

在这种情况下,您可以这样做

For this specific case you could just do

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

这将仅匹配一种重载方法(采用Object的方法).大概方法调用json.toString很简单,因为我们已经有了String.

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.

这篇关于Java库中对重载定义的含糊不清的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆