这是在jmeter中运行http请求后获得的json数据。
{
"entry": [
{
"search": {
"mode": "match"
},
"resource": {
"meta": {
"lastUpdated": "2018-10-29T08:37:53.351+00:00",
"versionId": "aa1e1cc3-8018-408e-a70a-4885004720fa",
"tag": [
{
"system": "http:\/\/hl7.org\/fhir\/v3\/ObservationValue",
"code": "SUBSETTED"
}
]
},
"name": [
{
"given": [
"Aaron697"
],
"use": "official",
"prefix": [
"Mr."
],
"family": "Abernathy524"
}
],
"id": "176b8fef-5a62-48da-b82e-4722843d9f2d",
"resourceType": "Patient"
},
"fullUrl": "http:\/\/localhost:4080\/Patient\/176b8fef-5a62-48da-b82e-4722843d9f2d"
},
{
"search": {
"mode": "match"
},
"resource": {
"meta": {
"lastUpdated": "2018-10-29T08:09:33.67+00:00",
"versionId": "c852310e-8894-45dd-a87c-9e449950caf4",
"tag": [
{
"system": "http:\/\/hl7.org\/fhir\/v3\/ObservationValue",
"code": "SUBSETTED"
}
]
},
"name": [
{
"given": [
"Aaron697"
],
"use": "official",
"prefix": [
"Mr."
],
"family": "Abernathy524"
}
],
"id": "8bbb3bb7-4e5b-48bd-82af-8463c4060de3",
"resourceType": "Patient"
},
"fullUrl": "http:\/\/localhost:4080\/Patient\/8bbb3bb7-4e5b-48bd-82af-8463c4060de3"
}
]
}
现在,我必须将所有ID和name.use,name.given,name.family,name.prefix提取到相应的csv文件中。 例如:id到id.csv文件中;将name.given转换为name_given.csv文件等... http请求可以包含n个ID和名称。怎么做? 我尝试过使用json提取和正则表达式失败。
答案 0 :(得分:1)
将以下代码放入“脚本”区域:
import com.jayway.jsonpath.JsonPath
def idCSV = new File('id.csv')
def nameGiven = new File('name_given.csv')
def nameFamily = new File('name_family.csv')
def namePrefix = new File('name_prefix.csv')
def newLine = System.getProperty('line.separator')
def response = prev.getResponseDataAsString()
def ids = JsonPath.read(response, '$..id')
ids.each { id ->
idCSV << id << newLine
}
def names = JsonPath.read(response, '$..name')
names.each { name ->
name.each { subName ->
subName.get('given').each { givenName ->
nameGiven << givenName << newLine
}
subName.get('prefix').each { prefix ->
namePrefix << prefix << newLine
}
nameFamily << subName.get('family') << newLine
}
}
就是这样,代码将从响应中提取所需的数据并将其存储到以下文件中
参考文献: