我必須在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」](例如) –