简体   繁体   English

为什么JSON中的中文字符会导致JSON.parse出现“控制不良”错误?

[英]Why do Chinese characters in JSON cause “bad control character” error with JSON.parse?

I have a standard HTML5-type client/server set up. 我有一个标准的HTML5类型的客户端/服务器设置。 The server side is all Java, and the client side is JavaScript. 服务器端都是Java,客户端是JavaScript。 Using ajax I send queries and receive replies. 使用ajax我发送查询并接收回复。 Up to now, I've had no problems with JSON.parse(data) . 到目前为止,我对JSON.parse(data)没有任何问题。 However, I have a new user who entered her last name using Chinese characters. 但是,我有一个新用户使用中文字符输入她的姓氏。 This is causing a "JSON.parse: bad control character in string literal" error on the client side. 这导致客户端“JSON.parse:字符串文字中的错误控制字符”错误。

The server builds a reply as follows (exception handling omitted): 服务器按如下方式构建回复(省略异常处理):

JSONObject jsono = new JSONObject();
jsono.put("last_name", last_name);
jsono.put("first-name", first_name);
String response = jsono.toString();

The client receives something like: 客户收到如下内容:

{"last_name":"Smith","first_name":"Bob"}

The reply is displayed on a web page which is set to <meta charset="utf-8"> : 回复显示在设置为<meta charset="utf-8">的网页上:

var theResult = JSON.parse(data);
$('#first_name').html(theResult.first_name);

This works just fine. 这很好用。 However, for the Chinese user, the client receives 但是,对于中国用户,客户端会收到

{"last_name":"唐","first_name":"Bob"}

and this causes the json.parse error. 这会导致json.parse错误。

I've now started looking at other characters. 我现在开始看其他角色了。 For example, Andrés does not cause an error, but also does not display properly. 例如, Andrés不会导致错误,但也无法正常显示。 It looks like Andr s . 它看起来像Andr s

So, I'm clearly missing something. 所以,我显然遗漏了一些东西。 Could someone enlighten me where the problem lies (eg, is it server side? client side? JavaScript? jquery? html?) and how to solve it? 有人可以告诉我问题所在(例如,它是服务器端吗?客户端?JavaScript?jquery?html?)以及如何解决它?

The most useful libraries in Java I have used are Gson API and JSONObject and both can handle this issue, then if you this your problem is probably solved. 我使用的Java中最有用的库是Gson APIJSONObject ,两者都可以解决这个问题,如果这样你的问题可能已经解决了。 just be careful all the utf-8 related params here are really important: 请注意这里所有与utf-8相关的参数非常重要:

JSONObject jsono = new JSONObject();
jsono.put("last_name", "唐");
jsono.put("first-name", firstName);
String myjsonString = jsono.toString();

//write your output
DataOutputStream out = new DataOutputStream(new FileOutputStream("myjson.txt"));
out.write(myjsonString.getBytes("utf-8"),0, myjsonString.getBytes("UTF-8").length);

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

相关问题 JSON.parse:JavaScript中出现意外字符错误 - JSON.parse: Unexpected character error in Javascript Angular / Spring 开机 | 错误:SyntaxError:“JSON.parse:JSON 数据的第 1 行第 1 列出现意外字符” - Angular / Spring Boot | error: SyntaxError: “JSON.parse: unexpected character at line 1 column 1 of the JSON data” JSON.parse(result.responseText)中的无效字符; - Invalid characters in JSON.parse(result.responseText); Grails:Linux上的JSON.parse()错误-在Windows上工作-已卡住 - Grails: JSON.parse() Error on Linux - Works on Windows - stumped JSON.parse Javascript中对Java对象抛出错误进行字符串化 - Stringifying a Java Object throwing Error in JSON.parse Javascript struts2和jquery文件上传SyntaxError:JSON.parse:意外字符 - struts2 and jquery file upload SyntaxError: JSON.parse: unexpected character SyntaxError:JSON.parse:期望的属性名称或&#39;}&#39; - SyntaxError: JSON.parse: expected property name or '}' Jongo vs(DBObject)JSON.parse - Jongo vs (DBObject)JSON.parse Java 6 ScriptEngine和JSON.parse问题 - Java 6 ScriptEngine and JSON.parse problem 使用itextpdf将.pptx转换为.pdf。 中文字符定位不好 - Converting .pptx to .pdf with itextpdf. Bad character positioning with chinese characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM