我有以下Groovy腳本(不是Grails應用程序),它返回類似JSON,但它不是嚴格有效的JSON。Groovy返回JSON
String baseURL = 'https://test.com'
File userFile = new File("./user.json")
def client = new HTTPBuilder(baseUrl)
client.headers['Content-Type'] = 'application/json'
client.request(GET, JSON) { req ->
requestContentType = JSON
headers.Accept = 'application/json'
response.success = { resp, json ->
userFile.append json.toString()
println JsonOutput.toJson(json.toString())
}
}
我想創建一個JSON輸出文件。我嘗試過使用JsonOutput.prettyPrint,並查看了JsonBuilder,但是看起來像Groovy應該支持輸出時我必須手動構建JSON結構。這是我回來的。
{AssetNumber=AssetNumber1, DeviceFriendlyName=FriendlyName1, PhoneNumber=17035551231, SerialNumber=SerialNumber1, Udid=Udid1, [email protected], UserId=userId1, UserName=userName1}
正如我所說,這是類似JSON的,但並不嚴格有效。我期待的是這樣的:
{"AssetNumber": "AssetNumber1", "DeviceFriendlyName": "FriendlyName1"....}
任何想法?
您能否提供JSON內容您可以獲得測試嗎? – Opal 2014-10-06 15:08:43
我正在調用的Web服務在通過REST客戶端測試時返回有效的JSON。 @Opal,我不確定我瞭解你的問題。我正在提供JSON。僅供參考,我正在使用Groovy v2.3.6 – 2014-10-06 15:24:29
對不起。我認爲這只是你得到的一部分。看到我編輯的答案。 – Opal 2014-10-06 15:59:13