2016-07-25 12 views
1

私はUTF-8エンコーディングのファイルを持っています。UTF-8でエンコードされたデータでGroovy JsonOutput.toJsonを使用するには?

私は、JSON構造を持つファイルをロードし、それを修正し、それを保存するためのGroovyスクリプトを書く:

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がUNICODEにUTF-8データを変換することです。 JsonSlurper().parseがUTF-8を使用できる理由はわかりませんが、JsonOutput.toJsonは使用できません。

どのようにJsonOutput.toJsonにUTF-8を使用していますか? JsonSlurper().parse

答えて

0

私はエンコーディングが間違った文で適用されると信じています。

から文以下変更を行います。

def originFile = new File(originPreviewFilePath) 
def originPreview = new JsonSlurper().parse(originFile,'UTF-8') 

def originFile = new File(originPreviewFilePath).getText('UTF-8') 
def originPreview = new JsonSlurper().parseText(originFile) 
+1

これは効果がありません。問題は、 'JsonOutput.toJson'が入力とは無関係にユニコードを使用することです。 –

+0

それは私のために働いた。どうも –

関連する問題