gpt4 book ai didi

grails - groovy:将字符串(列表)转换为Groovy列表

转载 作者:行者123 更新时间:2023-12-02 15:03:28 25 4
gpt4 key购买 nike

import groovy.json.JsonSlurper

def ver = "['a', 'b', 'c']"
def jsonSlurper = new JsonSlurper()
def ver_list = jsonSlurper.parseText(ver)
println ver_list

这就是我在做什么。我想遍历 ver_list。而且似乎很难找到解决方案。

当我打印ver_list时,这是错误:
Caught: groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object

The current character read is ''' with an int value of 39
Unable to determine the current character, it is not a string, number, array, or object
line number 1
index number 1
['a', 'b', 'c']
.^
groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object

The current character read is ''' with an int value of 39
Unable to determine the current character, it is not a string, number, array, or object
line number 1
index number 1
['a', 'b', 'c']

最佳答案

有效的json字符串必须用双引号引起来。

因此更改此行中的代码即可使用:

def ver = '["a", "b", "c"]'

但是,如果您需要解析无效的json,则可以尝试LAX解析器。

那么甚至可以解析以下字符串
import groovy.json.JsonSlurper
import groovy.json.JsonParserType

def ver = "[a, 'b', 001]"
def jsonSlurper = new JsonSlurper().setType( JsonParserType.LAX )
def ver_list = jsonSlurper.parseText(ver)
println ver_list

关于grails - groovy:将字符串(列表)转换为Groovy列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60975012/

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