接口自动化之thirdDay JsonGuolvTest 过滤掉某个字段的值 price

来源:互联网 发布:数据透视表多条件筛选 编辑:程序博客网 时间:2024/05/22 00:53
package com.thirdDay;import org.junit.Test;import java.util.*;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * Created by Administrator on 2017/8/19 0019. */public class JsonGuolvTest {    //过滤掉某个字段的值 price    @Test    public void GuoLv() {        String result = "{\n" +                "    \"code\": 200,\n" +                "    \"data\": {\n" +                "        \"isExist\": false,\n" +                "        \"dataList\": [\n" +                "            {\n" +                "                \"currency\": \"USD\",\n" +                "                \"@@iPrice@@\": 12\n" +                "            },\n" +                "            {\n" +                "                \"currency\": \"EUR\",\n" +                "                \"iPrice\": 34\n" +                "            },\n" +                "            {\n" +                "                \"currency\": \"CNY\",\n" +                "                \"iPrice\": 56\n" +                "            }\n" +                "        ],\n" +                "        \"time\": 1453193714993,\n" +                "        \"pid\": 78630\n" +                "    },\n" +                "    \"success\": true\n" +                "}\n";        String returnBody = "{\n" +                "    \"code\": 200,\n" +                "    \"data\": {\n" +                "        \"isExist\": false,\n" +                "        \"dataList\": [\n" +                "            {\n" +                "                \"currency\": \"USD\",\n" +                "                \"iPrice\": 13\n" +                "            },\n" +                "            {\n" +                "                \"currency\": \"EUR\",\n" +                "                \"iPrice\": 34\n" +                "            },\n" +                "            {\n" +                "                \"currency\": \"CNY\",\n" +                "                \"iPrice\": 56\n" +                "            }\n" +                "        ],\n" +                "        \"time\": 1453193714993,\n" +                "        \"pid\": 78630\n" +                "    },\n" +                "    \"success\": true\n" +                "}\n";        Map<String,String> stringStringMap = new HashMap<String, String>();        stringStringMap.put("hopeResult",result);        stringStringMap.put("runResult",returnBody);        formatData(stringStringMap);        String hopeResult = stringStringMap.get("hopeResult");        String runResult = stringStringMap.get("runResult");        if(hopeResult.equalsIgnoreCase(runResult)){            System.out.print("pass");        }else{            System.out.print("fail");        }    }    /**     * @param stringStringMap     * @auto:jiaou     * @return     * @Description 去掉@@符号里面的数据     */    public static void formatData(Map<String, String> stringStringMap) {        //预期的结果        String hopeResult = stringStringMap.get("hopeResult");        //实际的结果        String runResult = stringStringMap.get("runResult");        //正则表达式 过滤        Pattern p = Pattern.compile("@@(.*?)@@");        Matcher m = p.matcher(hopeResult);        List<String> keyList = new ArrayList<String>();        StringBuffer stringBuffer = new StringBuffer();        StringBuffer stringBuffer2 = new StringBuffer();        //把所有加了@@属性的名称拿到        while (m.find()) {            Integer start = m.start();            Integer end = m.end();            String key = hopeResult.substring(start, end);            key = key.replace("@@", "");            keyList.add(key);        }        HashSet<String> hs = new HashSet<String>(keyList);        Iterator<String> iterator = hs.iterator();        //遍历key        while (iterator.hasNext()) {            String key = iterator.next();            hopeResult = stringStringMap.get("hopeResult");            runResult = stringStringMap.get("runResult");            hopeResult = hopeResult.replace("@@", "");            stringStringMap.put("hopeResult", sp(hopeResult, key));            stringStringMap.put("runResult", sp(runResult, key));        }    }    /**     * @param     * @return     * @Description 通过关键字切割数据     */    public static String sp(String hopeResult, String key) {        //是否是json格式校验        int type = 0; //0 是JSON        if (key.startsWith("<") && key.endsWith(">")) {            type = 1;        }        String[] strings = hopeResult.split(key);        String temp = "";        for (int i = 0; i < strings.length; i++) {            //当i=0的时候 那么这组数据是头数据            if (i == 0) {                temp += strings[i];            } else {                //如果i>0  要做数据处理                String ss = strings[i];                if (type == 0) {                    temp += byJson(ss);                } else {                    temp += byXML(key, ss);                }            }        }        return temp;    }    /**     * @param     * @return     * @Description 去掉XML格式数据     */    public static String byXML(String key, String re) {        key = "</" + key.substring(1);        re = re.substring(re.indexOf(key) + key.length());        return re;    }    /**     * @param     * @return     * @Description 去掉JSON格式数据     */    public static String byJson(String re) {        byte[] bytes = re.getBytes();        int i = 0;        for (Byte b : bytes) {            //,44  }125  ]93            if (b.intValue() == 44 || b.intValue() == 125 || b.intValue() == 93) {                break;            }            i += 1;        }        re = re.substring(i);        return re;    }}

阅读全文
0 0