在将JSON文件与JSON响应

时间:2018-05-29 10:47:21

标签: json groovy soapui

我正在比较SOAPUI中的JSON响应和JSON文件(ver - 4.6.4,免费软件) Groovy脚本:

import groovy.json.JsonSlurper
import java.io.File

def ResponseMessage = testRunner.testCase.testSteps["Fi - Request 1"].testRequest.response.contentAsString

def jsonResp = new JsonSlurper().parseText(ResponseMessage)

def jsonFile = new File("C:/Users/new_2.json")
def InputJSON = new JsonSlurper().parseFile(jsonFile, 'UTF-8')

assert jsonResp.equals(InputJSON)

但是每次运行弹出异常都会抛出:

  

groovy.lang.MissingMethodException:没有方法签名:   groovy.json.JsonSlurper.parseFile()适用于参数类型:   (java.io.File,java.lang.String)值:[C:\ Users \ new_2.json,UTF-8]   可能的解决方案:第7行的prseText(java.lang.String)错误

它也不适用于parseText。 将其视为初学者脚本。

现在我面临全新的错误 -

  

groovy.json.JsonException:Lexing在线失败:1,列:1,在阅读''时,   没有可能的有效JSON值或标点符号可以被识别。错误在第9行

JSON文件内容的第1行: {

"Metadata": {
    "DocType": "Report",
    "SubType": "",
    "Content": {
        "Title": "Economic Comment",
        "Headline": "",
        "Summary": "",
        "Blurb": ""

    },
    "Priority": "1",
    "DocumentIDs": {
        "DocumentID": {
            "label": "",
            "type": "",
            "level": "",
            "value": ""
        }
    },

注意:此内容不完整。由于安全原因,我无法在此处粘贴完整的JSON

1 个答案:

答案 0 :(得分:1)

错误信息清楚地说明了替代方案。

更改以下声明:

def InputJSON = new JsonSlurper().parseFile(jsonFile, 'UTF-8')

要:

def InputJSON = new JsonSlurper().parse(jsonFile, 'UTF-8')

documentation

引用JsonSlurper API