Groovy JSON 转换为 CSV

Groovy JSON convert to CSV

我在 groovy 中通过 JsonSlurper 解析 Json 时遇到问题。你能帮忙吗? 我的示例 json 是:

{
   "expand": "schema,names",
   "startAt": 0,
   "maxResults": 50,
   "total": 21,
   "issues":    [
            {
         "expand": "operations,versionedRepresentations",
         "id": "217580",
         "self": "issue/217580",
         "key": "ART-4070",
         "fields": {"summary": "#[ART] Pre.3 Verification \"S\""}
      },
            {
         "expand": "operations,versionedRepresentations",
         "id": "217579",
         "self": "issue/217579",
         "key": "ART-4069",
         "fields": {"summary": "Verification \"C\""}
      },
            {
         "expand": "operations,versionedRepresentations",
         "id": "217577",
         "self": "issue/217577",
         "key": "ART-4068",
         "fields": {"summary": "#[ART] Enum type"}
      },
   ]
}

在结果中我期待一个 csv 文件 关键列表,仅摘要:

key,summary
ART-4070,#[ART] Pre.3 Verification \"S\"
ART-4069,Verification \"C\"
ART-4068,#[ART] Enum type

当我尝试开始代码解析时 json 我有一个错误: 线程“main”中的异常 groovy.json.JsonException:期待“}”或“,”但得到当前 char 'S' 的 int 值为 83

当前读取的字符是'S',int值为83 期待 '}' 或 ',' 但得到当前 char 'S' 的 int 值为 83 第 13 行 索引号 321 "fields": {"summary": "#[ART] Pre.3 验证 "S""}

代码:

import groovy.json.*

def jsonSlurper = new JsonSlurper()
def json = '''
{
   "expand": "schema,names",
   "startAt": 0,
   "maxResults": 50,
   "total": 21,
   "issues":    [
            {
         "expand": "operations,versionedRepresentations",
         "id": "217580",
         "self": "issue/217580",
         "key": "ART-4070",
         "fields": {"summary": "#[ART] Pre.3 Verification \"S\""}
      },
            {
         "expand": "operations,versionedRepresentations",
         "id": "217579",
         "self": "issue/217579",
         "key": "ART-4069",
         "fields": {"summary": "Verification \"C\""}
      },
            {
         "expand": "operations,versionedRepresentations",
         "id": "217577",
         "self": "issue/217577",
         "key": "ART-4068",
         "fields": {"summary": "#[ART] Enum type"}
      },
   ]
}
'''
def obj = jsonSlurper.parseText(json)
println obj

请帮忙

首先:''' 字符串中需要双 \;第二个是 , 在列表的最后一个元素上。

import groovy.json.JsonSlurper

def jsonSlurper = new JsonSlurper()
def json = '''
{
   "expand": "schema,names",
   "startAt": 0,
   "maxResults": 50,
   "total": 21,
   "issues":    [
            {
         "expand": "operations,versionedRepresentations",
         "id": "217580",
         "self": "issue/217580",
         "key": "ART-4070",
         "fields": {"summary": "#[ART] Pre.3 Verification \"S\""}
      },
            {
         "expand": "operations,versionedRepresentations",
         "id": "217579",
         "self": "issue/217579",
         "key": "ART-4069",
         "fields": {"summary": "Verification \"C\""}
      },
            {
         "expand": "operations,versionedRepresentations",
         "id": "217577",
         "self": "issue/217577",
         "key": "ART-4068",
         "fields": {"summary": "#[ART] Enum type"}
      }
   ]
}
'''
def obj = jsonSlurper.parseText(json)
println obj