接口自动化之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
- 接口自动化之thirdDay JsonGuolvTest 过滤掉某个字段的值 price
- 接口自动化之thirdDay JsonArrayTest 顺序变化的时候
- 接口自动化之thirdDay JsonPathTest 定位
- SQL Server 中创建一个函数(过滤某个字段值里面的html标签)
- Sphinx按某个字段搜索过滤
- sql取某个字段相近某个值的记录
- 怎样让新建的Answer必须对某个字段进行过滤?
- fastjson 过滤掉多余的字段
- oracle 拼接某个字段的所有值
- oracle 拼接某个字段的全部值
- 检查某个字段的值是否重复
- 取出list里面某个字段的值
- sqlserver 过滤掉某些字段查询剩余字段的方法
- 接口自动化之json取值
- 接口自动化之 JsonArrayTest
- 接口自动化测试系列之PHPUnit-接口测试的步骤
- mysql如何更新一个表中的某个字段值等于另一个表的某个字段值
- 将A表中某个字段的值赋给B表某个字段
- sysConfig.bat 闪退问题及此处不应有 \Java\jdk1.7.0_79问题的解决
- 使用python的BeautifulSoup库解析页面应选择适当容错能力的解析器
- openoffice问题:conversion failed: could not save output document; OOo errorCode: 283
- Android 三大图片缓存原理、特性对比
- 1159. 【NOI2002】银河英雄说(并查集)
- 接口自动化之thirdDay JsonGuolvTest 过滤掉某个字段的值 price
- 2017中国大学生程序设计竞赛
- Mapreduce二次排序实例
- Spring-AOP 自动创建代理
- 为MacOS升级vim至最新版
- 界面打飞机游戏,C语言
- c++常用知识点2
- Delphi Class of
- springMVC