我必须在Web应用程序中编写一个小脚本。这个网络应用程序有它的局限性,但类似于这个在线控制台:https://groovyconsole.appspot.com/所以如果它在这里工作,它也应该为我的问题。如何在Groovy中获得REST响应?
我需要解析一个JSON响应。为了简单起见,我开发了C#自己的Web API,当我在浏览器上输入链接(http://localhost:3000/Test)就给出了这样的字符串:
{"Code":1,"Message":"This is just a test"}
我想这个字符串,事后解析它,我想用JsonSplunker 。之后的研究时间,最引人注目的样品会是这样:
import groovyx.net.http.RESTClient
def client = new RESTClient('http://www.acme.com/')
def resp = client.get(path : 'products/3322') // ACME boomerang
assert resp.status == 200 // HTTP response code; 404 means not found, etc.
println resp.getData()
(从这里取:http://rest.elkstein.org/2008/02/using-rest-in-groovy.html)
但是它不承认import groovyx.net.http.RESTClient
。你可以尝试在提供的groovy web sonsole中测试它,你会得到错误。
我试过import groovyx.net.http.RESTClient.*
但仍然没有成功。
您可能不需要使用外部JSON解析器。似乎'groovyx.net.http.RESTClient'返回已经解析了JSON的'response.data'对象。尝试使用'response.data.keySet()'获取顶级密钥列表。然后'response.data.blah'返回一个特定的键值。 – MarkHu
@MarkHu感谢您的评论!我正在使用JsonSlurper,它工作。解析: inputedMemberID == resultMap [“MemberID”](例如) –