字符的邻是没有得到显示在我的DDL,我怎么能告诉RESTClient实现利用特定的字符集? [英] Characters Å Ä Ö is not getting displayed in my DDL, how can I tell restclient to utilize a specific charset?
问题描述
在我这里开始就是问题所在。它应该是这样的:
Before I start here is the problem. It should be like this:
比约恩尼尔森,而不是它的显示奇怪的特殊字符,有字符,A型和O变成这个样子。所有值
Björn Nilsson, instead its displaying strange special characters, all values that have characters Å, Ä and Ö becomes like this.
我填写我的DDL与XML格式的API,具有所有值的值,我们也使用Linq2Rest为。
I fill my DDL with values from API in XML format that has all the values, and we are also using Linq2Rest for that.
这怎么看起来像
private readonly RestContext<ADConsultants> restContext;
public ConsultantContext(Uri uri, Format format)
{
restContext = new RestContext<ADConsultants>(GetRestClient(uri, format), GetSerializerFactory(format));
}
public enum Format
{
Pox,
Json
}
private static readonly IEnumerable<Type> knownTypes = new[] {typeof (ADConsultants)};
public static IRestClient GetRestClient(Uri uri, Format format)
{
switch (format)
{
case Format.Pox:
return new XmlRestClient(uri);
case Format.Json:
return new JsonRestClient(uri);
default:
throw new NotImplementedException();
}
}
private static ISerializerFactory GetSerializerFactory(Format format)
{
switch (format)
{
case Format.Pox:
return new XmlSerializerFactory(knownTypes);
case Format.Json:
return new JsonNetSerializerFactory();
default:
throw new NotImplementedException();
}
}
public IQueryable<ADConsultants> Consultant
{
get { return restContext.Query; }
}
}
这是我的JsonNetSerializerFactory类:
This is my JsonNetSerializerFactory class:
public class JsonNetSerializerFactory :ISerializerFactory
{
public ISerializer<T> Create<T>()
{
return new JsonNetSerializer<T>();
}
public class JsonNetSerializer<T> : ISerializer<T>
{
public T Deserialize(string input)
{
return JsonConvert.DeserializeObject<T>(input);
}
public IList<T> DeserializeList(string input)
{
return JsonConvert.DeserializeObject<IList<T>>(input);
}
}
}
这是我的控制器内的:
And this is inside my controller:
var consultants = new ConsultantContext(
new Uri("http://adress:port/api/consultants"),
ConsultantContext.Format.Json)
.Consultant
.Where(x => x.Office == "Örebro")
.OrderBy(x => x.DisplayName)
.ToList()
.Select(x => new
{
name = x.DisplayName
});
我做这个做了一个试验:
I did a test by doing this:
name = "åäö"
和它工作得很好,DDL值分别为AAO
and it worked fine, ddl values was "åäö"
任何帮助是pciated如何解决这样的字符邻正常工作的价值在我的DDL AP $ P $。
Any help is appreciated on how to fix so characters Å Ä Ö works fine as values in my DDL.
HTTP头是utf-8,我的HTML内容有它以及。它必须是需要设置一个特定的字符集的XML,我该怎么办呢?
HTTP header is utf-8, my html content have it aswell. It must be the XML that need to be set to a specific charset, How can I do that?
在此先感谢!
推荐答案
在你有一个charset编码/解码问题的理论。
in the theory you got an charset encoding/decoding problem.
原因:您尝试读取已连接使用,如ISO-8859-1或ISO-8859-15字符集codeD的内容。你会尝试直接读取(德code)有一个UTF-8字模型。当然,它不会工作,因为UTF-8,因为UTF-8也不会创造奇迹识别您的特殊字符(A,U,O,等..)。 UTF-8字符编码没有猜测者。
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.
解决方案
1-(RE)EN code你的内容(如比约恩·尼尔森)及其相应的字符集(ISO-8859-1 / ISO-8859-15)成字节集合。
1- (Re)encode your content( e.g "Björn Nilsson") with its corresponding charset (iso-8859-1/iso-8859-15) into Byte collection.
2 - 德code您的内容为UTF-8的基础字符集。
2- Decode your content with into "UTF-8" based charset.
在这里的助手类为例:
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);
}
}
}
用法:
在你(JSON-,XML等)串行器/解串器类只是转换你的内容一样,
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);
}
}
}
这是你的
JsonNetSerializerFactory
请尝试其他工厂做同样喜欢
please try to do same for other factories like
XmlSerializerFactory
和在HTML页面不要忘记你的设置
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-->
这篇关于字符的邻是没有得到显示在我的DDL,我怎么能告诉RESTClient实现利用特定的字符集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!