This following code showing ?????, when I giving multi byte language Ex : Chinese
import groovy.json.JsonSlurper
def jsonObj = new JsonSlurper().parseText( '{ "name":"怎么样", "age": 23}')
println jsonObj.name
println jsonObj.age
Output :
???????
23
Do I need to do any encoding and decoding to process this ?
If Yes, how I need to pass that value as a Json argument to above ParseText method ?
As per the below API, there is a method where you may pass arguments to specify the Charset.
http://docs.groovy-lang.org/latest/html/gapi/groovy/json/JsonSlurper.html
Below method may be useful. Convert your text to byte array and use below method.
parse(byte[] bytes, String charset)
Another scenario that I can think of is that your source file is encoded using a charset which is not the default charset of your OS.
正如Jon Skeet所说:您需要一个具有Unicode的控制台(例如eclipse的grep控制台: http : //marian.schedenig.name/projects/grep-console/ )才能正确格式化输出。
It isn't a json issue at all. Json implementation here works with old good java 2 bytes unicode chars. Hieroglyphs aren't a problem for it. The problem is in your output encoding. Is it a Windows or Mac console?
Proof code:
def jsonObj = new JsonSlurper().parseText( '{ "name":"怎么样", "age": 23}')
Writer unicodeFileWriter = new OutputStreamWriter( new FileOutputStream("herogliph.txt"), "UTF-8");
unicodeFileWriter.write(jsonObj.toString());
unicodeFileWriter.close()
The resulting herogliph.txt contains a valid name.
Try something like this.
PrintStream out = new PrintStream(System.out, true, "UTF-8");
out.println(unicodeMessage);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.