Fórum

UTF-8 from JSON not displaying correctly

thumbnail
Ken Driscoll, modificado 11 Anos atrás.

UTF-8 from JSON not displaying correctly

Junior Member Postagens: 57 Data de Entrada: 02/07/12 Postagens Recentes
Hi all, I am building a portlet and am reading in a json using google's "gson" classes. I am then displaying this information on a jsp. Unfortunately, it currently is displaying special characters as �. This is obviously not what I want, so please take a look at the code below and let me know any ideas you have on remedying this situation.

Reading in the JSON (which has special characters that diplay correctly if viewing the straight json in a browser):

//jsonURL is the url string where the json is located
//urlParams are the params I am sending the service via post.
        URL url = new URL(jsonURL);
		HttpURLConnection httpcon = (HttpURLConnection) ((url.openConnection()));
		httpcon.setDoOutput(true);
		httpcon.setDoInput(true);
		httpcon.setInstanceFollowRedirects(false);
		httpcon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		httpcon.setRequestProperty("charset", "utf-8");
		httpcon.setRequestMethod("POST");
		httpcon.setRequestProperty("content-length", Integer.toString(urlParams.getBytes().length));
		DataOutputStream wr = new DataOutputStream(httpcon.getOutputStream ());
		wr.writeBytes(urlParams);
		wr.flush();

		//////////////////////////////////////////
		// Read JSON data back
		//////////////////////////////////////////
		BufferedReader reader = new BufferedReader (new InputStreamReader(httpcon.getInputStream(), "UTF-8"));
        reader = callProxy(IPortletConstants.PROXY_URL+"getContactProfile.htm","json="+contactJson);
		WrapperJSON wrapper = new Gson().fromJson(reader, WrapperJSON.class);
		ProfileJSON profile = wrapper.profile;
		actionRequest.setAttribute("profile", profile);


Then in my jsp:
<%@ include file="/init.jsp" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="ISO-8859-1"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
&lt;%
	ProfileJSON profile = (ProfileJSON) renderRequest.getAttribute("profile");
%&gt;

<p> &lt;%=profile.city%&gt; </p>


What displays on the page is M�nchen when it should be Mūnchen. I've tried adding UTF-8 anywhere I could with no luck. Any help would be greatly appreciated.