Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
6
rated 0 times [  10] [ 4]  / answers: 1 / hits: 9246  / 11 Years ago, sat, december 28, 2013, 12:00:00

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?


More From » java

 Answers
0

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);

[#49168] Friday, December 27, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
micayla

Total Points: 148
Total Questions: 92
Total Answers: 109

Location: Aruba
Member since Sat, Oct 2, 2021
3 Years ago
micayla questions
Fri, Dec 24, 21, 00:00, 2 Years ago
Thu, Apr 16, 20, 00:00, 4 Years ago
Thu, Nov 14, 19, 00:00, 5 Years ago
;