从Json Response获取节点 [英] Getting node from Json Response

查看:143
本文介绍了从Json Response获取节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Json响应:

  {
0P0000G5ZQ:[
{
Title:PIMCO Unconstrained Bond Inst,
ResourceId:587723,
PublicationTime:2013-03-07 14:13:00 -0600,
Type:Research,
Href:null,
VideoThumbnail:null,
},
{
Title: 非传统债券101,
ResourceId:609234,
PublicationTime:2013-08-27 06:00:00 -0500,
AuthorName: Josh Charney,
Type:News
VideoThumbnail:null,
},
{
Title:利率上升,
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,
}
],
0P0000P ZCB:[],
0P00002PYR:[
{
Title:FPA新收入,
ResourceId:578826,
PublicationTime:2012-12-26 00:00:00 -0600,
AuthorName:Sarah Bush,
Type:Research,
Href :null,
VideoThumbnail:null,
},
{
标题:FPA新收入公司2013年第二季度电话会议,
ResourceId:BWIPREM_20130719005736,
PublicationTime:2013-07-19 12:32:00 -0500,
来源:美国商业资讯,
输入:新闻,
SubType:BWIPREMBWIPREM,
VideoThumbnail:null,
AuthorThumbnail:null
}

]
}

我需要从响应ex打印下列节点:0P0000G5ZQ ,0P0000PZCB,0P00002PYR等,然后在这些节点的每一个中,如果标题节点存在,我需要断言这些节点。响应节点:0P0000G5ZQ,0P0000PZCB,0P00002PYR会根据我正在运行的服务而不断变化,所以我需要始终从响应中获取它,而不是对其进行硬编码。我已经尝试使用Json Slurper来获取节点:0P0000G5ZQ,0P0000PZCB,0P00002PYR如下所示:

  import com.eviware.soapui.support.XmlHolder 
import org.apache.commons.lang .StringUtils
import groovy.json.JsonSlurper
$ b 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

但这会返回以下INFO:null。

任何人都可以提供示例代码这个?感谢。

解决方案

  def slurp = new groovy.json.JsonSlurper()。parseText jsonStr)
slurp.each {key,val - >
val.each {
在it.keySet()
}
}
中声明标题

其中 jsonStr 表示上面的字符串中的json响应。 (附在'''yourJson'''


I have the following Json response:

 {
    "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
    }

    ]
    }

I need to print the following nodes from the response ex:"0P0000G5ZQ", "0P0000PZCB", "0P00002PYR" etc and then within each of these nodes I need to assert if the "Title" node is present. The response nodes:"0P0000G5ZQ", "0P0000PZCB", "0P00002PYR" keep on changing depending on the service I am running so I need to always obtain it from the response and not hardcode it. I have to do this in a script assertion in SoapUI.

I have tried to use Json Slurper to get the nodes:"0P0000G5ZQ", "0P0000PZCB", "0P00002PYR" etc as follows:

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

but this returns the following INFO:null.

Can anyone provide me a sample code to do this? Thanks.

解决方案

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

where jsonStr represents the above json response in string. (enclosed in '''yourJson''')

这篇关于从Json Response获取节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆