【发布时间】:2016-11-28 22:06:05
【问题描述】:
我有一个 UTF-8 编码的文件。
我写了一个 groovy 脚本来加载一个 JSON 结构的文件,修改它并保存它:
def originPreviewFilePath = "./xxx.json"
//target the file
def originFile = new File(originPreviewFilePath)
//load the UTF8 data file as a JSON structure
def originPreview = new JsonSlurper().parse(originFile,'UTF-8')
//Here is my own code to modify originPreview
//Convert the structure to JSON Text
def resultPreviewJson = JsonOutput.toJson(originPreview)
//Beautify JSON Text (Indent)
def finalFileData = JsonOutput.prettyPrint(resultPreviewJson)
//save the JSONText
new File(resultPreviewFilePath).write(finalFileData, 'UTF-8')
问题在于JsonOutput.toJson 将 UTF-8 数据转换为 UNICODE。我不明白为什么JsonSlurper().parse 可以使用UTF-8 而不是JsonOutput.toJson?
如何让JsonOutput.toJson 使用UTF-8?我需要与JsonSlurper().parse 完全相反
【问题讨论】:
标签: json unicode encoding groovy utf-8