如何将响应与 json 文件进行比较,并输出它们之间不匹配的项目

问题描述 投票:0回答:0

我有一个预期的 json 文件如下:

{
    "contact_info": [{
        "contact_id": "Contact_001",
        "contact_status": {
            "exam_status": 0,
            "term_list": [{
                "term_code": 110,
                "t_list": [{
                        "s_code": 12001,
                        "sexam_status": 0
                    },
                    {
                        "s_code": 13001,
                        "sexam_status": 1
                    }
                ]
            }]
        }
    }]
}

回复如下:

{
    "contact_info": [{
        "contact_id": "Contact_001",
        "contact_status": {
            "exam_status": 0,
            "term_list": [{
                "term_code": 110,
                "t_list": [{
                        "s_code": 12001,
                        "sexam_status": 0
                    }
                ]
            }]
        }
    }]
}

如何比较预期文件与响应以及它们之间不匹配的输出项目?

我尝试了以下方法,但输出文件不符合预期

//获取json对象中的所有键名

public static List<String> getAllKeysInJsonUsingJsonNodeFieldNames(String json) {
        ObjectMapper mapper = new ObjectMapper()
        List<String> keys = new ArrayList<>();
        JsonNode jsonNode = mapper.readTree(json);
        getAllKeysUsingJsonNodeFields(jsonNode, keys);
        return keys;
    }
    public static void getAllKeysUsingJsonNodeFields(JsonNode jsonNode, List<String> keys) {
        if (jsonNode.isObject()) {
            Iterator<Map.Entry<String, JsonNode>> fields = jsonNode.fields();
            fields.forEachRemaining({def field ->
                keys.add(field.getKey());
                getAllKeysUsingJsonNodeFields((JsonNode) field.getValue(), keys);
            });
        } else if (jsonNode.isArray()) {
            ArrayNode arrayField = (ArrayNode) jsonNode;
            arrayField.forEach({def node ->
                getAllKeysUsingJsonNodeFields(node, keys);
            });
        }
    }

//获取json对象中的所有键值

public static List<String> getAllKeysInJsonUsingJsonNodeFieldValues(String json) {
        ObjectMapper mapper = new ObjectMapper()
        List<String> keys = new ArrayList<>();
        JsonNode jsonNode = mapper.readTree(json);
        getAllKeysUsingJsonNodeFieldsValues(jsonNode, keys);
        return keys;
    }

    public static void getAllKeysUsingJsonNodeFieldsValues(JsonNode jsonNode, List<String> keys) {
        if (jsonNode.isObject()) {
            Iterator<Map.Entry<String, JsonNode>> fields = jsonNode.fields();
            fields.forEachRemaining({def field ->
                keys.add(field.getValue());
                getAllKeysUsingJsonNodeFieldsValues((JsonNode) field.getValue(), keys);
            });
        } else if (jsonNode.isArray()) {
            ArrayNode arrayField = (ArrayNode) jsonNode;
            arrayField.forEach({def node ->
                getAllKeysUsingJsonNodeFieldsValues(node, keys);
            });
        }
    }

//比较2个文件的方法

public static compareFile(String file_path, String actualJson, String expectedJson, String name, String api) {


        String diffFileName = new SimpleDateFormat('yyyyMMddHHmm\'.json\'').format(new Date())

        List<String> actualTagName = new ArrayList<String>()

        List<String> actualValue = new ArrayList<String>()

        List<String> expectedTagName = new ArrayList<String>()

        List<String> expectedValue = new ArrayList<String>()

        JSONObject  diffExpected = new JSONObject()

        JSONObject diffActual = new JSONObject()

        actualTagName = Common.getAllKeysInJsonUsingJsonNodeFieldNames(actualJson)
        actualValue = Common.getAllKeysInJsonUsingJsonNodeFieldValues(actualJson)
        expectedTagName = Common.getAllKeysInJsonUsingJsonNodeFieldNames(expectedJson)
        expectedValue = Common.getAllKeysInJsonUsingJsonNodeFieldValues(expectedJson)

        for(int i = 1; i < expectedTagName.size(); i++) {

            if(actualTagName.contains(expectedTagName[i])) {

                int index = actualTagName.indexOf(expectedTagName[i])
                String markex, markac = 'false'
                if(actualValue[index]!=expectedValue[i]) {
                    if(actualValue[index] == null) {actualValue[index] = "null"}

                    if(expectedValue[i] == null) {expectedValue[i] = "null"}
                    if (!expectedValue[i].toString().contains('\"')) {
                        markex = 'true'
                    }
                    if(expectedValue[i].toString().contains('\"')) {
                        expectedValue[i] = expectedValue[i].toString().replace('\"', '')
                    }
                    if(markex == 'true')
                    {
                        diffExpected.put(expectedTagName[i], Long.parseLong(expectedValue[i].toString()))
                    }
                    else {
                        diffExpected.put(expectedTagName[i],expectedValue[i])
                    }
                    if (!actualValue[index].toString().contains('\"')) {
                        markac = 'true'
                    }
                    if(actualValue[index].toString().contains('\"')) {
                        actualValue[index] = actualValue[index].toString().replace('\"', '')
                    }


                    if(markac == 'true')
                    {
                        diffActual.put(actualTagName[index], Long.parseLong(actualValue[index].toString()))
                    }
                    else {
                        diffActual.put(actualTagName[index], actualValue[index])
                    }
                }
            }
            else {
                String markex, markac = 'false'

                if(expectedValue[i] == null)
                {
                    expectedValue[i] = "null"
                }
                if (!expectedValue[i].toString().contains('\"')) {

                    markex = 'true'
                }
                if(expectedValue[i].toString().contains('\"')) {

                    expectedValue[i] = expectedValue[i].toString().replace('\"', '')
                }


                if(markex == 'true')
                {
                    diffExpected.put(expectedTagName[i], Long.parseLong(expectedValue[i].toString()))
                }
                else {
                    diffExpected.put(expectedTagName[i],expectedValue[i])
                }
            }
        }
        for(int i = 0; i < actualTagName.size(); i++) {

            if(!expectedTagName.contains(actualTagName[i])) {
                String markex, markac = 'false'


                if(actualValue[i] == null)

                {actualValue[i] = "null"}
                if (!actualValue[i].toString().contains('\"')) {

                    markac = 'true'
                }
                if(actualValue[i].toString().contains('\"')) {

                    actualValue[i] = actualValue[i].toString().replace('\"', '')
                }

                if(markac == 'true')
                {
                    diffActual.put(actualTagName[i], Long.parseLong(actualValue[i].toString()))
                }
                else {
                    diffActual.put(actualTagName[i], actualValue[i])
                }
            }
        }

        if(diffActual.toString() != "{}" || diffExpected.toString() != "{}") {

            FileWriter writer = new FileWriter(file_path + '\\Data Files\\' + api +'\\diffFile\\' + diffFileName)

            ObjectMapper mapper = new ObjectMapper()

            JSONObject obj = new JSONObject()

            obj.put('expected', diffExpected)

            obj.put('actual', diffActual)

            writer.write(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(mapper.readTree(obj.toString())))

            writer.close()

            status = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(mapper.readTree(obj.toString()))
        }
    }

//输出不匹配如下:

{
    "actual": {
        "contact_status": "{exam_status:0,term_list:[{term_code:110,t_list:[{S_code:12001,sexam_status:0}]}]}",
        "t_list": "[{S_code:12001,sexam_status:0}]",
        "term_list": "[{term_code:110,t_list:[{S_code:12001,sexam_status:0}]}]",
        "sexam_status": 0,
        "S_code": 12001
    },
    "expected": {
        "contact_status": "{exam_status:0,term_list:[{term_code:110,t_list:[{S_code:12001,sexam_status:0,:0},{S_code:13001,sexam_status:1}]}]}",
        "t_list": "[{S_code:12001,sexam_status:0},{S_code:13001,sexam_status:1}]",
        "term_list": "[{term_code:110,t_list:[{S_code:12001,sexam_status:0},{S_code:13001,sexam_status:1}]}]",
        "sexam_status": 1,
        "S_code": 13001
    }
}

但我希望输出应该如下所示=>你能告诉我如何解决吗?

{
  "actual" : {},
  "expected" : {
    "t_list" : "[{S_code:12001,sexam_status:0},{S_code:91002,sexam_status:1}]"
  }
}
java arrays json compare katalon-studio
© www.soinside.com 2019 - 2024. All rights reserved.