使用 Groovy 脚本从 JSON 响应中一一打印特定节点的所有值
Print all values of specific node one by one from a JSON response using Groovy script
我有一个来自 Web 服务的 JSON 响应,如下所示:
{
"status" : true,
"statusCode" : "OK",
"requestId" : "b9c0ffe3-2b62-465d-bc0f-48a1279c3a54",
"responseData" : {
"ResDoc" : {
"education" : {
"#text" : ["EDUCATION\n\n", ". I have a ", " in ", " with Leisure (", ")"],
"daterange" : [{
"start" : {
"@days" : "727200",
"@iso8601" : "1992-01-01",
"#text" : "1992"
},
"#text" : "-",
"end" : {
"@days" : "727200",
"@iso8601" : "1992-01-01",
"#text" : "1992"
}
}, {
"start" : {
"@days" : "727566",
"@iso8601" : "1993-01-01",
"#text" : "1993"
},
"#text" : "-",
"end" : {
"@days" : "728661",
"@iso8601" : "1996-01-01",
"#text" : "1996"
}
}, {
"start" : {
"@days" : "728661",
"@iso8601" : "1996-01-01",
"#text" : "1996"
},
"#text" : "-",
"end" : {
"@days" : "729757",
"@iso8601" : "1999-01-01",
"#text" : "1999"
}
}
],
"description" : ["During this period of time I obtained 8 GCSE's all above grade C. \tThese \tinclude Maths and English.", "I gained three A' levels all at grade C. These included Business, \tFinance, and Economics."],
"degree" : {
"@level" : "16",
"@name" : "Bachelor of Arts",
"#text" : "BA Honours Degree"
},
"major" : {
"@code" : "4399",
"#text" : "Business Studies"
},
"gpa" : "2.2"
}
}
}
}
我试图在 SOAPUI 中编写一个 Groovy 脚本来打印 daterange 部分下 'start' 和 'end' 节点的所有值。
我使用了下面的 Groovy 脚本,但我得到的是空值。
def response = messageExchange.response.responseContent
def list = new JsonSlurper().parseText(response).(responseData).(ResDoc)
log.info("===========Education Start Tag Section============")
def eduStartTags=list.education.daterange.start
eduStartTags.each{
log.info(it.value)
}
log.info("===========Education End Tag Section============")
def eduEndTags=list.resume.education.daterange.end
eduEndTags.each{
log.info(it.value)
}
有人可以帮我解决这个问题吗?我想一个一个打印开始和结束标签的所有值。
提前致谢。
它将是:
def parsed = new JsonSlurper().parseText(json)
parsed.responseData.ResDoc.education.daterange.each {
println "start $it.start, end: $it.end"
}
或打印 start
和 end
集合:
println parsed.responseData.ResDoc.education.daterange.start
println parsed.responseData.ResDoc.education.daterange.end
我有一个来自 Web 服务的 JSON 响应,如下所示:
{
"status" : true,
"statusCode" : "OK",
"requestId" : "b9c0ffe3-2b62-465d-bc0f-48a1279c3a54",
"responseData" : {
"ResDoc" : {
"education" : {
"#text" : ["EDUCATION\n\n", ". I have a ", " in ", " with Leisure (", ")"],
"daterange" : [{
"start" : {
"@days" : "727200",
"@iso8601" : "1992-01-01",
"#text" : "1992"
},
"#text" : "-",
"end" : {
"@days" : "727200",
"@iso8601" : "1992-01-01",
"#text" : "1992"
}
}, {
"start" : {
"@days" : "727566",
"@iso8601" : "1993-01-01",
"#text" : "1993"
},
"#text" : "-",
"end" : {
"@days" : "728661",
"@iso8601" : "1996-01-01",
"#text" : "1996"
}
}, {
"start" : {
"@days" : "728661",
"@iso8601" : "1996-01-01",
"#text" : "1996"
},
"#text" : "-",
"end" : {
"@days" : "729757",
"@iso8601" : "1999-01-01",
"#text" : "1999"
}
}
],
"description" : ["During this period of time I obtained 8 GCSE's all above grade C. \tThese \tinclude Maths and English.", "I gained three A' levels all at grade C. These included Business, \tFinance, and Economics."],
"degree" : {
"@level" : "16",
"@name" : "Bachelor of Arts",
"#text" : "BA Honours Degree"
},
"major" : {
"@code" : "4399",
"#text" : "Business Studies"
},
"gpa" : "2.2"
}
}
}
}
我试图在 SOAPUI 中编写一个 Groovy 脚本来打印 daterange 部分下 'start' 和 'end' 节点的所有值。 我使用了下面的 Groovy 脚本,但我得到的是空值。
def response = messageExchange.response.responseContent
def list = new JsonSlurper().parseText(response).(responseData).(ResDoc)
log.info("===========Education Start Tag Section============")
def eduStartTags=list.education.daterange.start
eduStartTags.each{
log.info(it.value)
}
log.info("===========Education End Tag Section============")
def eduEndTags=list.resume.education.daterange.end
eduEndTags.each{
log.info(it.value)
}
有人可以帮我解决这个问题吗?我想一个一个打印开始和结束标签的所有值。
提前致谢。
它将是:
def parsed = new JsonSlurper().parseText(json)
parsed.responseData.ResDoc.education.daterange.each {
println "start $it.start, end: $it.end"
}
或打印 start
和 end
集合:
println parsed.responseData.ResDoc.education.daterange.start
println parsed.responseData.ResDoc.education.daterange.end