简体   繁体   中英

Print all values of specific node one by one from a JSON response using Groovy script

I have a JSON response from a web service as below:

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

I was trying to write a Groovy script in SOAPUI to print all values of 'start' and 'end' nodes under daterange section. I used the below Groovy script, but I am getting null values.

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)
    } 

Can someone please help me out in resolving this issue. I wanted to print all values of start and end tags one by one.

Thanks in Advance.

It will be:

def parsed = new JsonSlurper().parseText(json)
parsed.responseData.ResDoc.education.daterange.each {
    println "start $it.start, end: $it.end"
}

Or to print the start and end collections:

println parsed.responseData.ResDoc.education.daterange.start
println parsed.responseData.ResDoc.education.daterange.end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM