【问题标题】:Json file parsing - GroovyJson 文件解析 - Groovy
【发布时间】:2020-09-10 11:18:55
【问题描述】:

对于我们的测试数据,我们将以 JSON 格式维护测试数据,下面是示例 JSON 文件。

{
    "INV": [
        {
            "TestCaseID": "APL",
            "TestData": [
                {
                    "INVLabel": "Title",
                    "Controltype": "text",
                    "Inputvalues": "solid state device"
                },
                {
                    "INVLabel": "Inventor",
                    "Controltype": "search",
                    "Inputvalues": "Sabra-Anne Truesdale"
                }
            ]
        },
        {
            "TestCaseID": "APL1",
            "TestData": [
                {
                    "INVLabel": "Title",
                    "Controltype": "text",
                    "Inputvalues": " "
                },
                {
                    "INVLabel": "Inventor",
                    "Controltype": "search",
                    "Inputvalues": "jenifer"
                }
            ]
        }
    ]
}

使用 JsonSlurper 如何根据测试用例 id 检索测试数据? 最初,我计划将 JSON 转换为我尝试过的 Map Object。但它没有按预期工作。谁能指导我检索这些值?

【问题讨论】:

    标签: grails groovy


    【解决方案1】:

    您可以在带有闭包{ it.TestCaseID == 'ID'}INV 节点上使用find() 方法从列表中获取第一个元素。考虑以下示例,该示例提取 id APL1 的测试数据。

    import groovy.json.JsonSlurper
    
    def input = '''{
        "INV": [
            {
                "TestCaseID": "APL",
                "TestData": [
                    {
                        "INVLabel": "Title",
                        "Controltype": "text",
                        "Inputvalues": "solid state device"
                    },
                    {
                        "INVLabel": "Inventor",
                        "Controltype": "search",
                        "Inputvalues": "Sabra-Anne Truesdale"
                    }
                ]
            },
            {
                "TestCaseID": "APL1",
                "TestData": [
                    {
                        "INVLabel": "Title",
                        "Controltype": "text",
                        "Inputvalues": " "
                    },
                    {
                        "INVLabel": "Inventor",
                        "Controltype": "search",
                        "Inputvalues": "jenifer"
                    }
                ]
            }
        ]
    }'''
    
    def json = new JsonSlurper().parseText(input)
    
    def testData = json.INV.find { it.TestCaseID == 'APL1' }.TestData
    
    println testData
    

    输出:

    [[INVLabel:Title, Controltype:text, Inputvalues: ], [INVLabel:Inventor, Controltype:search, Inputvalues:jenifer]]
    

    【讨论】:

    • 谢谢,使用文件解析我得到以下错误 -org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parseText () 适用于参数类型:(groovy.json.internal.LazyMap)
    • :我的代码 - @Keyword def getTestData(fileName){ def inputFile = new File(fileName) def InputJSON = new JsonSlurper().parseText(inputFile.text) def json = new JsonSlurper()。 parseText(InputJSON) def testData = json.INV.find { it.TestCaseID == 'APL' }.TestData println testData }
    • 代替JsonSlurper.parseText(),您可以使用parse() 方法与文件 - new JsonSlurper().parse(new File(fileName))
    猜你喜欢
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 2013-05-26
    • 2011-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多