gpt4 book ai didi

grails - 如何从 Controller 中的参数中提取列表

转载 作者:行者123 更新时间:2023-12-02 14:25:50 38 4
gpt4 key购买 nike

我有一个从列表中传入的列表列表,如下所示:

[lists:[["e","q"],["t","w"]], action:save, controller:newReport]

在我的 Controller 中
def lists = params.lists;

列表将是一个字符串,其值为“[[” e“,” q“],[” t“,” w“]]”

我应该如何遍历列表以获取每个子列表?

最佳答案

使用JSON.parse:

groovy:000> lists = JSON.parse("[['e', 'q'], ['w', 'q']]")
===> [["e","q"],["w","q"]]
groovy:000> lists.each { list -> println list }
[e, q]
[w, q]
===> [["e","q"],["w","q"]]
groovy:000> lists[0].each { entry -> println entry }
e
q
===> ["e","q"]

这比GroovyShell安全得多。这样,您不必考虑如何保护它或如何使用正则表达式来解析字符串。

关于grails - 如何从 Controller 中的参数中提取列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13491600/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com