5

I have a standard HTML5-type client/server set up. The server side is all Java, and the client side is JavaScript. Using ajax I send queries and receive replies. Up to now, I've had no problems with 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.

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">:

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.

I've now started looking at other characters. For example, Andrés does not cause an error, but also does not display properly. It looks like Andr�s.

So, I'm clearly missing something. Could someone enlighten me where the problem lies (e.g., is it server side? client side? JavaScript? jquery? html?) and how to solve it?

7
  • actually it is not a JavaScript problem, I think you use Java on the server-side, and it is related to your JSONObject API not client-side, if you really use Java add the tag then wait for my answer. Commented Dec 28, 2013 at 17:31
  • As Mehran said, it's not JSON or JavaScript. It's almost certainly an encoding problem. If the encoding is correct and everything knows what that encoding is, that works. (Example: jsbin.com/eParenUK/1/edit). So for example, if your JSON is returned to the browser using the Windows-1252 charset (or UTF-16, or...) instead of UTF-8... Commented Dec 28, 2013 at 17:33
  • I added the [java] tag. The server side is all Java. The production version runs on Linux; my development version runs on Mac.
    – RPW
    Commented Dec 28, 2013 at 17:45
  • How are you writing the response for the client? Commented Dec 28, 2013 at 18:29
  • I first write the header info (HTTP/1.0 200 OK Connection: close Server: ServerName Content-Type: text/html) and then try {output.writeBytes(response); Now that you mention it, I suspect text/html is relevant.
    – RPW
    Commented Dec 28, 2013 at 18:50

1 Answer 1

1

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. just be careful all the utf-8 related params here are really important:

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);
6
  • Not quite working yet. I'm using a DataOutputStream (called out) to return the response. I've added the headers out.writeBytes("Character-Encoding: utf-8"); and out.writeBytes("Content-Type: application/json; charset=utf-8");. I then call out.write(myjsonString.getBytes("UTF-8"), 0, myjsonString.getBytes("UTF-8").length); In the console, the object sent back looks fine, but this produces a JSON.parse: unexpected character error.
    – RPW
    Commented Dec 28, 2013 at 20:03
  • OK, got it working, but don't understand why! I changed the http headers back to txt/html but kept the out.write(myjsonString.getBytes("UTF-8"), 0, myjsonString.getBytes("UTF-8").length); statement. Now, both the Chinese characters and the accented e show up.
    – RPW
    Commented Dec 28, 2013 at 20:21
  • this is all about how to java web-servers make their HTTP responses, the important thing here is using the right character-encoding. have a good Christmas. :) Commented Dec 28, 2013 at 20:44
  • Downvoting because as I thought OP was not using servlet API, which is presented in this answer. DataOutputStream#writeBytes is writing only the first (lower) byte from string characters, that was the whole problem. docs.oracle.com/javase/7/docs/api/java/io/… Commented Dec 28, 2013 at 23:51
  • @Pavel Horal: thanks for the comment and downvote, I have updated my answer. Commented Dec 29, 2013 at 7:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.