使用Groovy脚本从JSON响应中逐个打印特定节点的所有值

时间:2015-10-19 09:34:53

标签: json grails groovy soapui

我有来自网络服务的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脚本来打印' start'的所有值。并且'结束' daterange部分下的节点。 我使用了下面的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)
    } 

有人可以帮我解决这个问题。我想逐个打印开始和结束标签的所有值。

先谢谢。

1 个答案:

答案 0 :(得分:2)

它将是:

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