我有来自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" 
} 

或打印 startend集合:
println parsed.responseData.ResDoc.education.daterange.start 
println parsed.responseData.ResDoc.education.daterange.end 


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!