2

我有以下 json 字符串:

[ { id: '123', name: 'bla bla', type: 'Source', isLeaf: true },
  { id: '3425', name: 'test test', type: 'Reference', isLeaf: false },
  { id: '12678', name: 'tags', type: 'Source', isLeaf: false },
]

我正在尝试使用 JsonSlurper 解析它,但出现错误:

groovy.json.JsonException: Lexing failed on line: 1, column: 5, while reading 'i', no possible valid JSON value or punctuation could be recognized.

我如何解析它并访问它id:'3425'

4

1 回答 1

4

您的 Json 无效,您需要使用双引号来分隔字符串,并且您还需要在 Json 中的键周围加上引号,如下所示:

[ { "id": "123", "name": "bla bla", "type": "Source", "isLeaf": true },
  { "id": "3425", "name": "test test", "type": "Reference", "isLeaf": false },
  { "id": "12678", "name": "tags", "type": "Source", "isLeaf": false },
]

然后你可以这样做:

def ids = new groovy.json.JsonSlurper().parseText( json ).id
assert ids[ 1 ] == '3425'
于 2013-04-25T13:45:28.570 回答