使用Groovy解析JSON,其中数组和多个对象没有要获取的名称列表

使用Groovy解析JSON,其中数组和多个对象没有要获取的名称列表,json,groovy,Json,Groovy,一般来说,groovy和编码都是新手。正在尝试执行以下操作: (我在stackoverflow中查看了许多以前的Q&A,但我发现没有一个解决方案有效) 我有以下JSON,我需要从中获得供应商名称的列表/字符串,即输出应该类似:“供应商1、供应商2、供应商3” 我使用以下groovy脚本打印列表中的所有供应商名称: import groovy.json.*; @CustomScriptAction( input = ['json_response'], output = 'sup

一般来说,groovy和编码都是新手。正在尝试执行以下操作: (我在stackoverflow中查看了许多以前的Q&A,但我发现没有一个解决方案有效)

我有以下JSON,我需要从中获得供应商名称的列表/字符串,即输出应该类似:“供应商1、供应商2、供应商3”

我使用以下groovy脚本打印列表中的所有供应商名称:

import groovy.json.*;

@CustomScriptAction(
    input = ['json_response'],
    output = 'suppliers'
)
def CustomScriptAction14()
{
    def object = new JsonSlurper().parseText(json_response.toString())
    def suppliers = "No suppliers"

if(object != null && !object.isEmpty())
{
    for(def i =0; i<object.size();i++)
{
    suppliers = RString.of(object[i].'supplier'.name.toString());

}
}
return suppliers
}
但我得到了一个空白的回答


我做错了什么

这最终奏效了:

import groovy.json.*;

@CustomScriptAction(
    input = ['json_response'],
    output = 'suppliers'
)


def customScript()
{

def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText(json_response.toString())
suppliers = RString.of(object.'supplier'.'name'.toString())

}
import groovy.json.*;

@CustomScriptAction(
    input = ['json_response'],
    output = 'suppliers'
)
def CustomScriptAction14()
{
    def object = new JsonSlurper().parseText(json_response)
    def suppliers = object.findAll { it.value instanceof List }
        .values()
        .flatten()
        .collect { [it.'supplier'.'name'] }
}
return suppliers
import groovy.json.*;

@CustomScriptAction(
    input = ['json_response'],
    output = 'suppliers'
)


def customScript()
{

def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText(json_response.toString())
suppliers = RString.of(object.'supplier'.'name'.toString())

}