json - 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
请帮忙
解决方案
第一:你需要双\\
内'''
字符串;其次,
,列表的最后一个元素上有一个。
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
推荐阅读
- mysql - 如何从 MySQL 数据库中获取用户数据并使用 Microsoft Bot Framework?
- java - Jenkins期间连接断开(主从结构)
- wordpress - 当执行以下钩子'wp_ajax_'时?
- python - 这是 Python 中的高效计算器吗?
- reactjs - 在 npm 包中使用 styled-component 时出现奇怪的问题
- javascript - Redux 程序 - 以下代码的输出未在 VS Code 终端中打印。没有错误但未打印输出
- windows - 尝试使用不同凭据从 powershell 运行 exe 时出错
- c - 将字符打印到C中的同一行
- angular - 将应用程序从 angular 8 转换为 angular 9 后,应用程序未使用 prod build 进行初始化
- vue.js - 如何在 vue 中使用弹性搜索?如何在弹性搜索中存储一些数据?