1

I want to populate utf-8 data which coming in the form of json from server to UI page.

I can see that "jsonStr" has utf-8 data. I have verified in debug mode in Eclipse.

But when I see json variable value it is showing as "????".

This is the code snippet which I used

 <script  language="JavaScript" charset="utf-8">
var json = <%=jsonStr%>
</script> 

Could anybody help me in this issue?

Even jQuery code snippets also fine

4
  • Are you displaying this in the browser? Seems the browser is not using the correct encoding. Commented May 23, 2012 at 13:42
  • i have set the encoding properly for browser.. because in other page i can see utf-8 chars. Commented May 23, 2012 at 13:44
  • Try to use utf-8 in your complete document i.e. the <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> Commented May 23, 2012 at 13:44
  • imdad - i have used <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> as well but still no use. Commented May 23, 2012 at 13:46

2 Answers 2

2

The issue may be that your server is not sending the data out as utf8. I'm not sure what server side language you're using, but this is what I do in PHP:

/**
 * encodes a string / array values to utf8
 * @param (mixed) $mixed
 */
static function utf8Encode( $mixed ) {
    if( is_array( $mixed ) ) {
        foreach( $mixed as $key => $value ) {
            $mixed[$key] = self::utf8Encode( $value );
        }
    } else {
        if( !mb_check_encoding( $mixed, 'UTF-8') ) {
            $mixed = utf8_encode( $mixed );
        }
    }

    return $mixed;
}
Sign up to request clarification or add additional context in comments.

2 Comments

<% is not unique to jsp, and I just started using this site and did not notice the tag
well, I didn't mean to be rude. Nonetheless, your answer is correct, only in another language;)
1

Tell JSP to write the response body using UTF-8 by adding the following line to top of JSP page:

<%@page pageEncoding="UTF-8" %>

To avoid repeating this for every single JSP page, you can also configure it in web.xml as follows so that it get automatically applied on every single JSP:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>

Note that the HTML meta Content-Type tag is ignored when the page is served over HTTP. It's only been used when the page is saved to client's disk and then reopend from it by a file:// URL.

See also:

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.