我在Ready!Api 1.9.0中使用Groovy腳本來解碼在SOAP響應中返回的base64字符串,並將生成的JSON對象存儲在json文件中。然後拿出結果文件並用JsonSlurper解析得到一個Map對象。如何迭代從JsonSlurper.parse(JSONFile)返回的Map對象?
該對象需要迭代,所以我可以找到一個鍵並聲明它的值。我無法弄清楚爲什麼沒有找到鑰匙。如果我直接使用map.get(key)調用密鑰,則會出現「沒有此類屬性」錯誤。如果我使用map.get('key')直接調用它,它將返回null。我也試過Map.each{k -> log.info("${k}")}
,它返回'interface.java.util.Map'而不是期望的鍵列表。在JSON解析之前,不完整的JSON雖然的
//create file path
def respFile = "C:\\Users\\me\\Documents\\Temp\\response.json"
//set originaldata in response to var
def response1 = context.expand('${Method#Response#declare namespace ns4=\'com/service/path/v4\'; declare namespace ns1=\'com/other/service/path/v4\'; //ns1:RequestResponse[1]/ns1:GetAsset[1]/ns1:Asset[1]/ns4:DR[1]/ns4:Sources[1]/ns4:Source[1]/ns4:OriginalData[1]}')
//decode the data
byte[] decoded = response1.decodeBase64()
//create file using file path above if it doesnt exist
def rf = new File(respFile)
//write data to file NOTE will overwrite existing data
FileOutputStream f = new FileOutputStream(respFile);
f.write(decoded);
f.close();
//begin second file
import groovy.json.JsonSlurper;
def inputFile = new File("C:\\Users\\me\\Documents\\Temp\\response.json")
def parResp = new JsonSlurper().parse(inputFile)
//test to find key
Map.each{k -> log.info("${k}")}
.. //示例:
{
"Response": {
"ecn": 1000386213,
"header": {
"msgRefNum": "bbb-ls-123"
},
"success": true,
"duplicatedit": false,
"subjectReturnCode": 1,
"subject": [
{
"uu": 11264448,
"name": {
"name3": "WINSTON BABBLE",
"dob": "19700422",
"gender": "2",
"ecCoded": "160824",
"ecCodeSegment": "ZZ"
},
"acc": [
{
"ftp": "01",
"Number": "AEBPJ3977L",
"issued": "20010101",
"mMode": "R"
} ],
"telephone": [
{
"telephoneType": "01",
"telephoneNumber": "9952277966",
"mMode": "R"
} ],
"address": [
{
"line1": "M\/O HEALTH AND FAMILY WELFARE",
"sCode": "07",
"cCode": 110009,
"ac": "04",
"reportedd": "160430",
"mMode": "R",
"mb": "lakjsdf blorb"
},
可以共享JSON的樣本? – Raphael
@Raphael我修改了原帖。 – justAguy88