Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何在groovy中断言数组json_Arrays_Json_Groovy_Jsonslurper - Fatal编程技术网

Arrays 如何在groovy中断言数组json

Arrays 如何在groovy中断言数组json,arrays,json,groovy,jsonslurper,Arrays,Json,Groovy,Jsonslurper,我的JSON响应如下所示 { "pCategories": [ "pogc1", "pogc16", "pogc2", "testc1122", "testcat10012018", "testcat10012019", "testcat100120191", "testcat11012018", "testcat12012018",

我的JSON响应如下所示

{
    "pCategories": [
        "pogc1",
        "pogc16",
        "pogc2",
        "testc1122",
        "testcat10012018",
        "testcat10012019",
        "testcat100120191",
        "testcat11012018",
        "testcat12012018",
        "testcat120120181",
        "testcat20112017",
        "testcat20112018"
    ]
}
我使用了下面的代码来断言

def slurped = new JsonSlurper().parseText(response.asString())
assert slurped.pCategories.contains("$category")
但是有一个错误


如何解析它?

因为$category不是字符串。它是GStringImpl的一个实例

def category = 'pogc16'
assert 'pogc16'.equals("$category") // false
要修复代码,可以将$category转换为字符串:

assert slurped.pCategories.contains("$category".toString())

这里需要一些帮助。现在还不清楚你到底想做什么

如果category是一个变量,那么您不必在contains中使用$category,您只需使用category,除非您正在计算某个表达式

但如果这不是您的用例,您只想消除错误;只需在$symbol之前添加转义字符:

assert slurped.pCategories.contains("\$category")

如果你能详细说明你的用例,也许我们能帮上忙。

但我还是在错误断言slurped.pCategories.contains$category.toString | | | | | | | | | | false | testcat21Feb20181144 | testcat21Feb20181144 |[pogc1、pogc16、pogc2、testc1122、testcat10012018、testcat10012019、testcat100120191、testcat11012018、testcat12012018、testcat120120181、testcat20112017、testcat20112018、testcat21feb20181142、testcat21feb20181144]在org.codehaus.groovy.runtime.InvokerHelper.assertFailedInvokerHelper.java:399在org.codehaus.groovy.runtime.ScriptBytecodeAdapter.assertFailedScriptBytecodeAdapter.java:648在a_orepim_getproduct_categories.getlistofcategoriesa_orepim_getproduct_getproduct_categories.groovy:129;.看看你的值。'testcat21Feb20181144'!='testcat21feb20181144“F”字母是的,我尝试将类别转换为.toLowerCase,然后它就可以工作了。谢谢