Characters Å Ä Ö is not getting displayed in my DDL, how can I tell restclient to utilize a specific charset?

前端 未结 2 594
滥情空心
滥情空心 2021-01-13 04:27

Before I start here is the problem. It should be like this:

Björn Nilsson, instead its displaying strange special characters, all values that have characters Å, Ä a

相关标签:
2条回答
  • 2021-01-13 04:44

    in the theory you got an charset encoding/decoding problem.

    the Cause: the content you try to read has been encoded using a charset like iso-8859-1 or iso-8859-15. and you'll try to read (decode) it directly to an "UTF-8" character Model. Of course it won't work because UTF-8 because UTF-8 won't in a miracle recognize your special chars (Ä,Ü, Ö, and so on..). UTF-8 is no guesser for character coding.

    Solution:

    1- (Re)encode your content( e.g "Björn Nilsson") with its corresponding charset (iso-8859-1/iso-8859-15) into Byte collection.

    2- Decode your content with into "UTF-8" based charset.

    here an Helper Class as example:

    using System;
    using System.Collections.Generic;
    using System.Text;
    
        namespace csharp.util.charset
        {
            public class SysUtil
            {
                /// <summary>
                /// Convert a string from one charset to another charset
                /// </summary>
                /// <param name="strText">source string</param>
                /// <param name="strSrcEncoding">original encoding name</param>
                /// <param name="strDestEncoding">dest encoding name</param>
                /// <returns></returns>
                public static String StringEncodingConvert(String strText, String strSrcEncoding, String strDestEncoding)
                {
                    System.Text.Encoding srcEnc = System.Text.Encoding.GetEncoding(strSrcEncoding);
                    System.Text.Encoding destEnc = System.Text.Encoding.GetEncoding(strDestEncoding);
                    byte[] bData=srcEnc.GetBytes(strText);
                    byte[] bResult = System.Text.Encoding.Convert(srcEnc, destEnc, bData);
                    return destEnc.GetString(bResult);
                }
    
            }
        }
    

    Usage:

    in your (JSON-, XML, other) serializer/deserializer classes just convert your content like that

    String content = "Björn Nilsson";
    SysUtil.StringEncodingConvert(content, "ISO-8859-1","UTF-8");
    

    you could try to make your calls in your deserializer (if they really do what they mean):

    public class JsonNetSerializerFactory :ISerializerFactory 
    {
        public ISerializer<T> Create<T>()
        {
            return new JsonNetSerializer<T>();
        }
        public class JsonNetSerializer<T> : ISerializer<T>
        {
            public T Deserialize(string input, String fromCharset, String toCharset)
    
            {
               String changedString = SysUtil.StringEncodingConvert(input, fromCharset,toCharset);
    
                return JsonConvert.DeserializeObject<T>(changedString  );
            }
    
            public IList<T> DeserializeList(string input, String fromCharset, String toCharset)
            {
             String changedString =  SysUtil.StringEncodingConvert(input, fromCharset,toCharset);
    
                return JsonConvert.DeserializeObject<IList<T>>(changedString);
            }
        }
    }
    

    that's for your

    JsonNetSerializerFactory  
    

    please try to do same for other factories like

    XmlSerializerFactory  
    

    and don't forget your setting in your HTML-page

    <meta charset="utf-8"> <!--HTML 5 -->
    
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <!-- if HTML version < 5-->
    
    0 讨论(0)
  • 2021-01-13 04:49

    There is most likely a mismatch between the character set of the text and what the browser interprets as the character set.

    Do you have a <meta> tag for content or charset?

    Given the character set you send is UTF-8, and you are using HTML5, this would be the appropriate tag

    <meta charset="utf-8">
    

    Or in earlier versions of HTML

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    

    For iso-8859-1 or any other charset, just replace the charset attribute.

    0 讨论(0)
提交回复
热议问题