简体   繁体   English

org.json字符编码/解码问题

[英]org.json character encoding/decoding issue

So I have a webservice call that is giving me back a JSON string. 因此,我有一个webservice调用正在给我返回JSON字符串。 I've requested that the response send be back the content in UTF-8 encoding. 我已要求响应发送回以UTF-8编码的内容。 When I inspect the value returned everything looks good. 当我检查返回的值时,一切看起来都很好。 It's when I create a new JSONObject and pass the JSON text into the constructor that things start to cause problems. 当我创建一个新的JSONObject并将JSON文本传递给构造函数时,事情开始引起问题。

Specifically: The webservice returns with an EM DASH (http://www.fileformat.info/info/unicode/char/2014/index.htm) character in the response - "format":"On Demand Event—All Sessions" 具体来说:该Web服务在响应中以EM DASH(http://www.fileformat.info/info/unicode/char/2014/index.htm)字符返回-“格式”:“按需事件-所有会话”

After the JSONObject receives the string it converts this character into the java representation of this character - "format":"On Demand Event\—All Sessions" JSONObject接收到字符串后,它将此字符转换为该字符的java表示形式-“ format”:“ On Demand Event \\ u2014All Sessions”

I am using the org.json library to do my bidding and am considering moving to a different implementation if necessary. 我正在使用org.json库进行出价,并考虑在必要时转向其他实现。 Here is what I've got running the show: 这是我主持的节目:

HttpClient client = new HttpClient();
GetMethod getDownloads = new GetMethod("http://localhost:8080/ecommerce-ws/service/account/downloads");

getDownloads.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
getDownloads.setRequestHeader("Accept", "application/json");

client.executeMethod(getDownloads);

String httpResponse = getDownloads.getResponseBodyAsString();

JSONObject downloadsJSON = new JSONObject(httpResponse);

Is there some way that I can set the character encoding on the JSONObject when it parses the string? 有什么方法可以在解析字符串时在JSONObject上设置字符编码? I've done quite a bit of looking around and have found very little that helps. 我已经做了很多环顾四周的工作,却发现几乎没有什么帮助。

Thank you in advance for your assistance. 预先感谢您的协助。

Good question. 好问题。

The JSON spec seems to allow escaping of all unicode chars JSON规范似乎允许转义所有Unicode字符

http://www.ietf.org/rfc/rfc4627.txt http://www.ietf.org/rfc/rfc4627.txt

but when I test, the GSON doesn't seem to escape the EM DASH 但是当我测试时,GSON似乎并没有逃脱EM DASH

http://code.google.com/p/google-gson/ http://code.google.com/p/google-gson/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM