使用JsonSlurper解析JSON文件
我的代码如下所示
String url = 'https://urlThatIWantToGoto'
String jsonFile = new JsonSlurper.parseText(new URL(url).text))
JSONArray jsonParse = new JSONArray(jsonFile)每当我运行这段代码时,我都会得到一个错误打印,如下所示
Caught: org.codehaus.groovy.grails.web.json.JSONException: Expected a ',' or '}' at character 982 of "MyJSONFile"有趣的是,它适用于我拥有的一个示例JSON url,而其他两个则失败。(我已经检查并确认了所有三个urls都包含有效的JSON文件)
有人能告诉我我的代码出了什么问题吗?
发布于 2015-06-02 19:08:27
我把这个搞定了。以下是代码:
def url = "https://gist.githubusercontent.com/lee910.../test2"
def jsonResponse = new JsonSlurper().parseText(new URL(url).text)
println "== ${jsonResponse.size()}: ${jsonResponse[0]}"因此,请注意,jsonResponse已经是一个数组,因此不需要JSONArray。印刷提供:
== 30: [course:[guid:2f093ff2-913b-4081-a800-238200f1c559], cr...最后一件事。确保您的睡眠器属于import groovy.json.JsonSlurper,以防万一。
https://stackoverflow.com/questions/30599237
复制相似问题