Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
Ken Driscoll
UTF-8 from JSON not displaying correctly
August 7, 2012 6:27 AM

Ken Driscoll

Rank: Junior Member

Posts: 57

Join Date: July 2, 2012

Recent Posts

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):
 2//jsonURL is the url string where the json is located
 3//urlParams are the params I am sending the service via post.
 4        URL url = new URL(jsonURL);
 5        HttpURLConnection httpcon = (HttpURLConnection) ((url.openConnection()));
 6        httpcon.setDoOutput(true);
 7        httpcon.setDoInput(true);
 8        httpcon.setInstanceFollowRedirects(false);
 9        httpcon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
10        httpcon.setRequestProperty("charset", "utf-8");
11        httpcon.setRequestMethod("POST");
12        httpcon.setRequestProperty("content-length", Integer.toString(urlParams.getBytes().length));
13        DataOutputStream wr = new DataOutputStream(httpcon.getOutputStream ());
14        wr.writeBytes(urlParams);
15        wr.flush();
17        //////////////////////////////////////////
18        // Read JSON data back
19        //////////////////////////////////////////
20        BufferedReader reader = new BufferedReader (new InputStreamReader(httpcon.getInputStream(), "UTF-8"));
21        reader = callProxy(IPortletConstants.PROXY_URL+"getContactProfile.htm","json="+contactJson);
22        WrapperJSON wrapper = new Gson().fromJson(reader, WrapperJSON.class);
23        ProfileJSON profile = wrapper.profile;
24        actionRequest.setAttribute("profile", profile);

Then in my jsp:
1<%@ include file="/init.jsp" %>
2<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="ISO-8859-1"%>
3<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
5    ProfileJSON profile = (ProfileJSON) renderRequest.getAttribute("profile");
8<p> <> </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.