我有以下 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 },
]
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 JsonSlurper 解析它,但出现错误:
groovy.json.JsonException: Lexing failed on line: 1, column: 5, while reading 'i', no possible valid JSON value or punctuation could be recognized.
Run Code Online (Sandbox Code Playgroud)
我如何解析它并访问id:'3425'?
您的 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 },
]
Run Code Online (Sandbox Code Playgroud)
然后你可以这样做:
def ids = new groovy.json.JsonSlurper().parseText( json ).id
assert ids[ 1 ] == '3425'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25567 次 |
| 最近记录: |