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

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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NeilNiu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值