我正在尝试将以下API中的JSON对象解析为groovy:
http://mtgapi.com/api/v1/fetch/id/1?token=f1fc6636e6f25d97c007984f0c7fe5785b3e3482
这是我的班级:
package mtgtournamentorganizer
import groovy.json.JsonSlurper
class GetCardService {
String token = "?token=f1fc6636e6f25d97c007984f0c7fe5785b3e3482"
String base = "http://mtgapi.com/api/v1/fetch/"
String id = "id/"
String cardId
String apiString
def getCardById(cardId) {
apiString =base + id + cardId + token
URL apiUrl = new URL(apiString)
def card = new JsonSlurper().parse(apiUrl)
return card
}
}
当我致电getCardById(1)
我收到此错误:
| groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parse() is applicable for argument types: (java.net.URL) values: [http://mtgapi.com/api/v1/fetch/id/1?token=f1fc6636e6f25d97c007984f0c7fe5785b3e3482]
Possible solutions: parse(java.io.Reader), use([Ljava.lang.Object;), wait(), any(), grep(), wait(long)
at mtgtournamentorganizer.GetCardService.getCardById(GetCardService.groovy:21)
答案 0 :(得分:15)
在我看来,您需要最新版本的Groovy
才能实现此功能(2.2.1
似乎没问题,但2.1.9
不是。同时(直到Groovy
升级,如果您收到的数据不是太大),您可以使用以下内容:
def card = new JsonSlurper().parseText(apiUrl.text)