Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Groovy 尝试从REST响应(SOAPUI)中提取以下值_Groovy_Soapui - Fatal编程技术网

Groovy 尝试从REST响应(SOAPUI)中提取以下值

Groovy 尝试从REST响应(SOAPUI)中提取以下值,groovy,soapui,Groovy,Soapui,我有以下SOAPUI rest输出: <html> <head> <meta content="HTML Tidy for Java (vers. 26 Sep 2004), see www.w3.org" name="generator"/> <title/> </head> <body> {"message":{},"records":[{"Application N

我有以下SOAPUI rest输出:

<html>
   <head>
      <meta content="HTML Tidy for Java (vers. 26 Sep 2004), see www.w3.org" name="generator"/>
      <title/>
   </head>
   <body>
      {"message":{},"records":[{"Application Name_2":"DO NOT DELETE: QA Regression Test","Status ID_5":"13160","Email_6":"","ListFiles Webservices_4":"
      <a download="download" href="/" target="_blank">Test.txt&lt;\/a>,</a>
      <a download="download" href="/" target="_blank">TestTwo.txt&lt;\/a>","# Index_1":"1","DownloadFile Webservices_3":"</a>
      <a download="download" href="/" target="_blank">Test.txt&lt;\/a>"}],"header":[{"index":"1","name":"# Index","numformat":"","type":"string"},{"index":"2","name":"Application Name","numformat":"","type":"string"},{"index":"3","name":"DownloadFile Webservices","numformat":"","type":"string"},{"index":"4","name":"ListFiles Webservices","numformat":"","type":"string"},{"index":"5","name":"Status ID","numformat":"","type":"string"},{"index":"6","name":"Email","numformat":"","type":"string"}]}</a>
   </body>
</html>
这就是我能够提取的:

[<a target=_blank href="/files/spool/493500/1133476/1_2866521_1133476_Test.txt?fs=1" download>Test.txt</a>]

但是,我只想提取-Test.txt

您要解析的值是来自Json元素之一的任意字符串值,因此没有标准方法直接使用slurper来解析

例如,在您的示例中,由于字符串值是Xml,因此可以使用XmlSlurper再次解析它以获得所需的值。以下是您案例的简化示例:

import groovy.json.JsonSlurper

def message = '''
 {"message":{},"records":[
    {"DownloadFile Webservices_3":"<a download='download' href='/' target='_blank'>Text.txt</a>"}]}

'''

def jsonSlurper = new JsonSlurper().parseText(message)
def fieldDownloadFile = jsonSlurper.records.'DownloadFile Webservices_3'

// since <a> is the root of your xml you can access the value without
// any path, if your xml was more complex you need a path like JsonSlurper
def anchorValue =  new XmlSlurper().parseText(fieldDownloadFile)

log.info anchorValue // prints Text.txt

好像有很多地方,你想要哪一个?@rao,有可能有第一个吗?
import groovy.json.JsonSlurper

def message = '''
 {"message":{},"records":[
    {"DownloadFile Webservices_3":"<a download='download' href='/' target='_blank'>Text.txt</a>"}]}

'''

def jsonSlurper = new JsonSlurper().parseText(message)
def fieldDownloadFile = jsonSlurper.records.'DownloadFile Webservices_3'

// since <a> is the root of your xml you can access the value without
// any path, if your xml was more complex you need a path like JsonSlurper
def anchorValue =  new XmlSlurper().parseText(fieldDownloadFile)

log.info anchorValue // prints Text.txt