使用groovy从json读取数据

时间:2017-11-21 16:29:08

标签: json rest groovy soapui

响应:

[

      {
      "salesChannelName": "Global Customers",
      "customerSegmentName": "Top Global Customers",
      "productSolutionName": "Network Solutions",
      "topology": "",EDIT:
      "productFamilyName": "Wavelengths",
      "customerScenarioName": "",
      "productQuestionId": "1"
   }
]

编辑:基于OP评论

我正在尝试检查'salesChannelName'是否正在返回字母或不使用groovy脚本。

1 个答案:

答案 0 :(得分:1)

在这里,请按行注释。

  • 定义实际数据的预期模式
  • 提取数据
  • 验证

脚本

import groovy.json.JsonSlurper
def jsonString = """[ 
      {
      "salesChannelName": "Global Customers",
      "customerSegmentName": "Top Global Customers",
      "productSolutionName": "Network Solutions",
      "topology": "",
      "productFamilyName": "Wavelengths",
      "customerScenarioName": "",
      "productQuestionId": "1"
   }
]"""

//Regular expression to check the data is characters
def pattern = "[a-z,A-Z,' ']+"

def json = new JsonSlurper().parseText(jsonString)

//Get the sales channel name
println json[0].salesChannelName

//verify
assert json[0].salesChannelName =~ pattern