json - How to use Groovy JsonOutput.toJson with data encoded with UTF-8? -
i have file utf-8 encoding.
i write groovy script load file json structure, modify , save it:
def originpreviewfilepath = "./xxx.json" //target file def originfile = new file(originpreviewfilepath) //load utf8 data file json structure def originpreview = new jsonslurper().parse(originfile,'utf-8') //here own code modify originpreview //convert structure json text def resultpreviewjson = jsonoutput.tojson(originpreview) //beautify json text (indent) def finalfiledata = jsonoutput.prettyprint(resultpreviewjson) //save jsontext new file(resultpreviewfilepath).write(finalfiledata, 'utf-8')
the problem jsonoutput.tojson
transforms utf-8 data unicode. don't understand why jsonslurper().parse
can use utf-8 not jsonoutput.tojson
?
how have jsonoutput.tojson
use utf-8? need have exact inverse of jsonslurper().parse
i believe encoding applied @ incorrect statement while reading itself.
change below statements :
def originfile = new file(originpreviewfilepath) def originpreview = new jsonslurper().parse(originfile,'utf-8')
to:
def originfile = new file(originpreviewfilepath).gettext('utf-8') def originpreview = new jsonslurper().parsetext(originfile)
Comments
Post a Comment