首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Groovy JSON转换为CSV

Groovy JSON转换为CSV
EN

Stack Overflow用户
提问于 2020-09-04 18:16:54
回答 1查看 186关注 0票数 0

我在groovy中解析Json by JsonSlurper时遇到了一个问题。你能帮忙吗?我的json示例如下:

代码语言:javascript
复制
{
   "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文件,其中仅包含键和摘要的列表:

代码语言:javascript
复制
key,summary
ART-4070,#[ART] Pre.3 Verification \"S\"
ART-4069,Verification \"C\"
ART-4068,#[ART] Enum type

当我尝试开始解析这个json代码时,我遇到了一个错误: Exception in thread "main“groovy.json.JsonException:期望'}‘或',’,但得到当前字符'S‘的int值为83

读取的当前字符是'S‘,INT值为83,预期为'}’或‘,’,但获得当前字符'S‘,INT值为83行号13索引号321“field”:{"summary":"#ART Pre.3验证"S""}

代码:

代码语言:javascript
复制
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

请帮帮忙

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-04 18:26:33

第一:在'''字符串中需要两个\\;第二,在列表的最后一个元素上有一个,

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63739195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档