首页 > 解决方案 > Groovy JSON 转换为 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,期望 '}' 或 '',但当前字符 '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

请帮忙

标签: jsongroovyexport-to-csv

解决方案


第一:你需要双\\'''字符串;其次, ,列表的最后一个元素上有一个。

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

推荐阅读