请帮我阅读UNICODE字符,因为它来自java中的属性文件。例如:如果我传递了“Account.label.register”这个键,它应该返回给我,而不是像“寄存器”那样的字符表示。这里是我的样本特性文件从属性文件读取Unicode字符java
file_ch.properties
Account.label.register = \u5BC4\u5B58\u5668
Account.label.login = \u767B\u5F55
Account.label.username = \u7528\u6237\u540D
Account.label.password = \u5BC6\u7801
谢谢。
嗨,我读的属性文件中使用下面的Java代码
@Override
public ResourceBundle getTexts(String bundleName) {
ResourceBundle myResources = null;
try {
myResources = ResourceBundle.getBundle(bundleName, getLocale());
} catch (Exception e) {
myResources = ResourceBundle.getBundle(getDefaultBundleKey(), getLocale());
}
return myResources;
}
使用上面的方法还是可以的很好,我正在中国文字。但是对于我的应用程序中的一些ajax请求,我需要在X-JSON头文件中传递中文文本。下面
HashMap<String, List<String>> map = new HashMap<String, List<String>>();
List<String> errors = new ArrayList<String>();
errors.add(str); /*ex: str = "无效的代码" , value taken from properties file through resource bundle*/
map.put("ERROR", errors);
JSONObject json = JSONObject.fromObject(map);
response.setCharacterEncoding("UTF-8");
response.setHeader("X-JSON", json.toString());
response.setStatus(500);
示例代码给出我传递英语例如STR =“无效代码” X-JSON头携带信息原样。但是,如果STR =“无效的代码”(中国或任何其他文本)X-JSON头承载文本作为空像下面是我得到
response :
connection:close
Content-Encoding:gzip
Content-Type:text/html;charset=UTF-8
Date:Wed, 08 Jun 2016 10:17:43 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
Vary:Accept-Encoding
X-JSON:{"ERROR":["Invalid Code"]}
响应然而,如果“错误”包含“中国”文本比如: “无效的代码”
响应:
connection:close
Content-Encoding:gzip
Content-Type:text/html;charset=UTF-8
Date:Wed, 08 Jun 2016 10:17:43 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
Vary:Accept-Encoding
**X-JSON:{"ERROR":[" "]}** /*expecting the response X-JSON:{"ERROR":["无效的代码"]}*/
作为中国文本即将为空的,我想通过X-JSON头象下面
发送的unicode的之后要评估X-JSON头象下面
var json;
try {
json = xhr.getResponseHeader('X-Json');
} catch (e) {
alert(e);
}
if (json) {
var data = eval('(' + json + ')');
decodeMsg(data);
}
function decodeMsg(message) {
var mssg = message;
var r = /\\u([\d\w]{4})/gi;
mssg = mssg.replace(r, function (match, grp) {
return String.fromCharCode(parseInt(grp, 16)); });
mssg = unescape(mssg);
return mssg;
}
请给点建议后,解析使用Javascript代码中的Unicode字符。谢谢。
请显示你到目前为止尝试过什么。 – AlexR
为什么你想要unicode逃脱? – Kayaman
感谢您立即回复。请参阅我的编辑帖子,其中介绍了有关从属性文件中读取Unicode字符的详细说明。 –