[英]groovy: convert string (which is a list) to a Groovy List
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.