确认responseBody包含postman中的部分字符串

时间:2017-10-26 15:40:22

标签: json substring postman

我是JSON的新手,并且无法找到我想要实现的示例。我循环遍历我的responseBody试图确认每个返回的设备模型包含“Tra”的部分文本。如何解析子字符串的值?

{
    "categories:[],
    "equipment":[
        {
            "model":"580 Trac Vac"
        },
        {
            "model":"4X2 Trail Gaitor"
        },
        {
            "model":"Trac Mass-Ter  40'"
        }]
}


//Confirm Model Partial
var jsonData = JSON.parse(responseBody);
var resultCount = jsonData.equipment.length;
for (i=0;i<resultCount;i++){
    var modelString = jsonData.equipment[i].model;
    tests["Resultset " +i+ " contains \"Tra\""] = modelString.has("Tra");
}

我可以做类似的事情,还是应该使用jq和regex组合?

2 个答案:

答案 0 :(得分:1)

您可以使用 byte[] buffer = new byte[(int)(in1.gerChannel().size() / 2)]; in1.read(buffer, 0, (int)(in1.gerChannel().size() / 2)); out.write(buffer);

执行此操作
modelString.indexOf("Tra")

或者你可以用一个只有//Confirm Model Partial var jsonData = JSON.parse(responseBody); var resultCount = jsonData.equipment.length; for (i=0;i<resultCount;i++){ var modelString = jsonData.equipment[i].model; if(modelString.indexOf("Tra") > 0) { tests["Resultset " +i+ " contains \"Tra\""] = modelString.has("Tra"); } }

的正则表达式来做

答案 1 :(得分:1)

以下是Regex针对“Tra”的几种变体的解决方案

var jsonData = JSON.parse(responseBody);
var resultCount = jsonData.equipment.length;
for (i=0;i<resultCount;i++){
    var modelString = jsonData.equipment[i].model;
    var substring = /(?:(?:(?:TRA))|(?:(?:Tra))|(?:(?:tra)(?:.*)))/;
    tests["Resultset " +i+ " contains some form of \"Tra\""] = 
        modelString.has(substring);
}