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
これは効果がありません。問題は、 'JsonOutput.toJson'が入力とは無関係にユニコードを使用することです。 –
それは私のために働いた。どうも –