I'm trying to parse the JSON object from the following API into groovy:
http://mtgapi.com/api/v1/fetch/id/1?token=f1fc6636e6f25d97c007984f0c7fe5785b3e3482
Here is my class:
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
}
}
When I call getCardById(1)
I get this error:
| 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)
Seems to me that you need a recent version of Groovy
for this to work (2.2.1
seems to be OK but 2.1.9
is not). In the mean time (until Groovy
is upgraded and if the data you are receiving is not too big) you could use something like this:
def card = new JsonSlurper().parseText(apiUrl.text)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With