【发布时间】:2012-07-23 08:42:35
【问题描述】:
我的页面上有一个 jstree 树结构,它使用 JSON 数据和 AJAX。有一些斯堪的纳维亚字符(ä 和 ö)处理不当。
Jstree 通过 Java servlet 过滤器获取 JSON 结构。该结构被编码为 UTF-8。当我用 firebug 查看返回的 JSON 结构时,斯堪的纳维亚字符显示正确。我尝试将字符编码更改为 ISO 8859-4 只是为了看看它是否有帮助,但它没有。
我不确定代码的哪些部分与此问题相关,但这里有一些部分。
初始化树:
.jstree({
"json_data" : {
"ajax" : {
"url" : hostUrl+"/json/getAreaTree?treeType=Areas",
"data" : function (n) {
return { id : n.attr ? n.attr("id") : 0 };
}
}
},
部分servlet过滤器代码:
protected class GetAreaTreeContext extends ActionContext implements StreamResponseContext{
private byte[] bytes;
public GetAreaTreeContext() {
super("getAreaTree");
}
@Override
public byte[] getBytes() {
return this.bytes;
}
@Override
public String getContentType() {
return "application/json; charset=UTF-8";
}
@Override
protected boolean doAction() {
if (!getWebSessionObject().isValid())
return false;
Map<String,Object> p = getParameterMap();
String type = (String)p.get("treeType");
String id = (String)p.get("id");
if(id.equals("1") || id.equals("0") || id.equals("id1") || id.equals("id0")){ //get the tree only if request comes from initial situation (id=0) or the root (id=1)
try {
this.bytes = ObjectFactory.getInstance().getDbManager().getAreaFolderTree(type, phone).getBytes();
} catch (Exception ex) {
this.result = "";
}
return bytes.length > 0;
}else{
//init the array again so that when empty folders make ajax requests, they dont get the tree
this.bytes = new byte[0];
return true;
}
}
}
如何让 jstree JSON_DATA 插件处理 UTF-8 编码的斯堪的纳维亚字符?
【问题讨论】:
-
Mhh...您是否尝试在 json_data>ajax 属性中添加 ' "contentType": "application/json charset=utf-8" '?
-
我试过了,什么都没做..
-
运气好能解决这个问题吗?我来自巴西,我们也有一些常见的特殊字符,例如 ç、ã 和 é。
-
嗯,这发生在很久以前,但 IIRC 是其他地方的字符串编码错误?不过我可能是错的..
标签: json character-encoding jstree