如何验证接口响应json字段值不为空
1个回答
展开全部
// 获取验证字段不为空
private String checkDataNotNull(List<String> lines, String key, String value) {
String content = lines.toString();
String value1 = value;// 存放一个value,因为后面回去value进行编辑
if (key == null || key.isEmpty() || lines.isEmpty()) {
return value1;
}
if (key.equalsIgnoreCase("notnull")) {
value = value.replace(",", ",");// 替换掉中文逗号
if (value.indexOf(",") == -1) {// 如果不存在逗号,则直接进入验证
if (!content.contains(value1)) {
return author;
}
for (int k = 0; k < lines.size(); k++) {
String line = lines.get(k);
if (line.contains(key + ":")) {
String lineValue = deleteCharFromString(key + ":", line);
if (lineValue == null || lineValue.isEmpty()) {
return author;
}
}
}
return value1;
} else {// 如果包含逗号,先将中文逗号转化为英文都好
List<String> list = new ArrayList<>();// 用list存放获取到的value中的值
for (int i = 0; i < 10; i++) {// for循环获取验证文本
if (value.indexOf(",") == -1) {// 如果已经到头,则直接赋值并添加list,跳出循环
String key1 = value;
list.add(key1);
break;
}
String key1 = value.substring(0, value.indexOf(","));// 截取字符串
list.add(key1);// 添加list
value = deleteCharFromString(key1 + ",", value);// 删除截取到的字符串
}
List<String> results = new ArrayList<String>();
for (int i = 0; i < list.size(); i++) {
String keys = list.get(i);
for (int k = 0; k < lines.size(); k++) {
String line = lines.get(k);
if (line.contains(key + ":")) {
String lineValue = deleteCharFromString(key + ":", line);
if (lineValue == null || lineValue.isEmpty()) {
results.add(author);
break;
}
}
}
results.add(keys);
}
StringBuffer text = new StringBuffer();
for (int i = 0; i < results.size(); i++) {
if (i == results.size() - 1) {
text.append(results.get(i));
continue;
}
text.append(results.get(i) + ",");
}
String texts = text.toString();
// output(texts, value1);
return texts;// 返回值
}
} else {
return author;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询