2015-10-19 34 views
1

我有如下一個Web服務的JSON響應的所有值:打印特定節點逐一從JSON響應使用Groovy腳本

{ 
    "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腳本打印日期範圍部分下的「開始」和「結束」節點的所有值。 我使用下面的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) 
    } 

是否有人可以幫我解決這個問題。我想逐個打印所有開始和結束標籤的值。

在此先感謝。

回答

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 
+1

謝謝了。有效。 – Velmurugan