Groovy JsonSlurper不一致地解析字符串?有时返回字符串,有时返回ArrayList?

Groovy JsonSlurper不一致地解析字符串?有时返回字符串,有时返回ArrayList?,groovy,jsonslurper,Groovy,Jsonslurper,我有一个操作对象,它在JSON中重复,但用于不同的上下文。该操作的convenienceType字段基本上来自枚举选择,因此无论生成操作对象的上下文如何,带有“视图”描述符的convenienceType都是相同的对象 我在Groovy脚本中使用JSONSlurper来解析它。以下是我看到的: 在背景层面: operation.convenienceType.descriptor//计算结果为“查看”(字符串) 在上下文B级别: operation.convenienceType.descrip

我有一个
操作
对象,它在JSON中重复,但用于不同的上下文。该操作的
convenienceType
字段基本上来自枚举选择,因此无论生成
操作
对象的上下文如何,带有
“视图”
描述符的convenienceType都是相同的对象

我在Groovy脚本中使用JSONSlurper来解析它。以下是我看到的:

在背景层面:

operation.convenienceType.descriptor//计算结果为“查看”(字符串)

在上下文B级别:

operation.convenienceType.descriptor//计算结果为[“视图”](数组)

所以当我试着做一些像

operation.convenienceType.descriptor.toLowerCase

它适用于上下文A,但在上下文B,我得到一个错误,因为我的代码抱怨数组没有
toLowerCase
方法


有人知道为什么会这样吗

您给出的示例不会以您指定的方式失败:

"context A": {
    "name": "parent A",
    "operation": {
        "name": "op A",
        "convenienceType": {
            "descriptor": "View",
            "id": "view_id"
        }
    },
    "context B": {
        "name": "child B",
        "operation": {
            "name": "op B",
            "convenienceType": {
                "descriptor": "View",
                "id": "view_id"
            }
        }
    }   
}

请添加代码以及显示该问题的确切JSON。您提供的一个似乎奇怪地嵌套,如果您尝试它,您将看到它是有效的,并且没有随机行为。最有可能的情况是,json中有数组,您使用的代码将返回到隐式扩展运算符调用。我必须相信,您的密钥链中的一个值是一个列表(使用[]),而不是一个对象(使用{})。即使列表中只有一个对象,它也会将计算中的类型转换为列表。
def j = '''{
"context A": {
    "name": "parent A",
    "operation": {
        "name": "op A",
        "convenienceType": {
            "descriptor": "View",
            "id": "view_id"
        }
    },
    "context B": {
        "name": "child B",
        "operation": {
            "name": "op B",
            "convenienceType": {
                "descriptor": "View",
                "id": "view_id"
            }
        }
    }   
}
}'''

import groovy.json.*

def p = new JsonSlurper().parseText(j)

assert p.'context A'.operation.convenienceType.descriptor == 'View'
assert p.'context A'.'context B'.operation.convenienceType.descriptor == 'View'