将字典序列化为JSON的奇怪结果
本文关键字:结果 JSON 字典 序列化 | 更新日期: 2023-09-27 18:11:49
我目前正在考虑将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
}
}
. .每个键的第一个字符被写成UTF-8代码。如果我在字符串前加上一个字母,我就不会遇到这个问题。(如d21.10.2011)
下面是我用来序列化字典的代码:公共类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 new AjaxDictionary<string, bool>(js);
}
#endregion
#region Nested type: AjaxDictionary
[Serializable]
public class AjaxDictionary<TKey, TValue> : ISerializable
{
private readonly Dictionary<TKey, TValue> _dictionary;
public AjaxDictionary()
{
_dictionary = new Dictionary<TKey, TValue>();
}
public AjaxDictionary(Dictionary<TKey, TValue> dic)
{
_dictionary = dic;
}
public AjaxDictionary(SerializationInfo info, StreamingContext context)
{
_dictionary = new Dictionary<TKey, TValue>();
}
public TValue this[TKey key]
{
get { return _dictionary[key]; }
set { _dictionary[key] = value; }
}
#region ISerializable Members
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
foreach (var key in _dictionary.Keys)
info.AddValue(key is string ? key as string : key.ToString(), _dictionary[key]);
}
#endregion
public void Add(TKey key, TValue value)
{
_dictionary.Add(key, value);
}
}
#endregion
}
编辑:给接受的答案添加注释:这对于javascript是正确的,但对于JSON就不是这样了。序列化器似乎有点狂热,不仅序列化为JSON,而且还确保JSON是javascript。添加完整的JSON语法,见https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/JSON
JSON = null
or true or false
or JSONNumber
or JSONString
or JSONObject
or JSONArray
JSONNumber = - PositiveNumber
or PositiveNumber
PositiveNumber = DecimalNumber
or DecimalNumber . Digits
or DecimalNumber . Digits ExponentPart
or DecimalNumber ExponentPart
DecimalNumber = 0
or OneToNine Digits
ExponentPart = e Exponent
or E Exponent
Exponent = Digits
or + Digits
or - Digits
Digits = Digit
or Digits Digit
Digit = 0 through 9
OneToNine = 1 through 9
JSONString = ""
or " StringCharacters "
StringCharacters = StringCharacter
or StringCharacters StringCharacter
StringCharacter = any character
except " or ' or U+0000 through U+001F
or EscapeSequence
EscapeSequence = '" or '/ or '' or 'b or 'f or 'n or 'r or 't
or 'u HexDigit HexDigit HexDigit HexDigit
HexDigit = 0 through 9
or A through F
or a through f
JSONObject = { }
or { Members }
Members = JSONString : JSON
or Members , JSONString : JSON
JSONArray = [ ]
or [ ArrayElements ]
ArrayElements = JSON
or ArrayElements , JSON
这是因为在JSON(或JavaScript)中键名(或JavaScript中的属性)不能以数字值开头,因此它将这些字符序列化后,以确保它们是正确的JSON格式