1

我有以下 Json 响应:

 {
    "0P0000G5ZQ":    [
    {
    "Title": "PIMCO Unconstrained Bond Inst",
    "ResourceId": "587723",
    "PublicationTime": "2013-03-07 14:13:00 -0600",
    "Type": "Research",
    "Href": null,
    "VideoThumbnail": null,
    },
    {
    "Title": "Nontraditional Bond 101",
    "ResourceId": "609234",
    "PublicationTime": "2013-08-27 06:00:00 -0500",
    "AuthorName": "Josh Charney",
    "Type": "News"
    "VideoThumbnail": null,
    },
    {
    "Title": "Investors on the Move as Rates Rise",
    "ResourceId": "607677",
    "PublicationTime": "2013-08-16 06:00:00 -0500",
    "AuthorName": "Christine Benz",
    "Type": "Video",
    "SubType": "MSTARVDVD",
    "VideoThumbnail":
    "http://im.mstar.com/im/videocenter/130814_flows_largethumb.jpg",
    }
    ],
    "0P0000PZCB": [],
    "0P00002PYR":    [
    {
    "Title": "FPA New Income",
    "ResourceId": "578826",
    "PublicationTime": "2012-12-26 00:00:00 -0600",
    "AuthorName": "Sarah Bush",
    "Type": "Research",
    "Href": null,
    "VideoThumbnail": null,
    },
    {
    "Title": "FPA New Income, Inc. 2nd Quarter 2013 Conference Call",
    "ResourceId": "BWIPREM_20130719005736",
    "PublicationTime": "2013-07-19 12:32:00 -0500",
    "Source": "Business Wire",
    "Type": "News",
    "SubType": "BWIPREMBWIPREM",
    "VideoThumbnail": null,
    "AuthorThumbnail": null
    }

    ]
    }

我需要从响应中打印以下节点,例如:“0P0000G5ZQ”、“0P0000PZCB”、“0P00002PYR”等,然后在每个节点中,我需要断言“标题”节点是否存在。响应节点:“0P0000G5ZQ”、“0P0000PZCB”、“0P00002PYR”根据我正在运行的服务不断变化,因此我需要始终从响应中获取它,而不是对其进行硬编码。我必须在 SoapUI 中的脚本断言中执行此操作。

我尝试使用 Json Slurper 来获取节点:“0P0000G5ZQ”、“0P0000PZCB”、“0P00002PYR”等,如下所示:

import com.eviware.soapui.support.XmlHolder
import org.apache.commons.lang.StringUtils
import groovy.json.JsonSlurper 

def holder = new XmlHolder(messageExchange.responseContentAsXml)
def response = messageExchange.response.responseContent
log.info response

def slurper = new JsonSlurper()
def json = slurper.parseText(response)
log.info json.each

但这会返回以下信息:null。

谁能给我一个示例代码来做到这一点?谢谢。

4

1 回答 1

2
def slurp = new groovy.json.JsonSlurper().parseText(jsonStr)
slurp.each{key, val ->
    val.each{
        assert "Title" in it.keySet()
    }
}

其中jsonStr以字符串形式表示上述 json 响应。(附在'''yourJson'''

于 2013-09-03T21:39:32.083 回答