我目前正在考虑使用WCF进行REST服务.我遇到的一个问题是序列化字典结果.我以前在建议的包装类这篇文章连载由字符串表示的日期(如"20.10.2011")和布尔变量的字典.当我测试结果时,我看到了这个:
{
"DeparturesResult":
{
"_x0032_1.10.2011":true,
"_x0032_4.10.2011":true,
"_x0032_6.10.2011":true,
"_x0032_8.10.2011":true,
"_x0033_1.10.2011":true
}
}
Run Code Online (Sandbox Code Playgroud)
..每个键中的第一个字符都写成UTF-8代码.如果我在字符串前加上字母,我不会遇到这个问题.(例如d21.10.2011)
这是我用来序列化字典的代码:public class FlService:IFlService {#region IFlService Members
public AjaxDictionary<string, bool> Departures(string from, string to, string portFrom, string portTo)
{
var startDate = DateTime.Today; // DateTime.ParseExact(from, "dd.MM.yyyy", null);
var endDate = DateTime.ParseExact(to, "dd.MM.yyyy", null);
var client = new Timetables();
var result = client.GetJourneys(startDate, endDate.AddDays(1), portFrom, portTo);
var js = result
.GroupBy(x => x.DepartureTime.CarResToDateTime())
.Select(x => x.Key)
.OfType<DateTime>()
.Select(x => x.Date)
.Distinct()
.ToDictionary(x => x.Date.ToString("dd.MM.yyy"), x => true);
return …Run Code Online (Sandbox Code Playgroud)