使用dictionary作为属性反序列化JSON将导致空.NET dictionary<;字符串,字符串>;

使用dictionary作为属性反序列化JSON将导致空.NET dictionary<;字符串,字符串>;,.net,json,serialization,dictionary,.net,Json,Serialization,Dictionary,我定义了一个WCF web服务方法,其中一个参数dict是一个Dictionary: JSON被愉快地反序列化,没有错误,但是dict的值为null(Count=0)。所有其他属性都已正确反序列化。我可以在跟踪日志中清楚地看到,IIS识别dict并正确地将其作为对象键入,但当我查看方法中的Dictionary变量dict时,它是空的 [HttpRequest] Method POST [HttpRequest] Content-Length 161 [HttpRequ

我定义了一个WCF web服务方法,其中一个参数
dict
是一个
Dictionary

JSON被愉快地反序列化,没有错误,但是
dict
的值为null(
Count=0
)。所有其他属性都已正确反序列化。我可以在跟踪日志中清楚地看到,IIS识别
dict
并正确地将其作为对象键入,但当我查看方法中的
Dictionary
变量
dict
时,它是空的

[HttpRequest] Method            POST
[HttpRequest] Content-Length    161
[HttpRequest] Content-Type      application/json; charset=utf-8
[HttpRequest] Authorization     Basic ZHNcYmFybmVzcjpSZWN0MWYxZXI=
[HttpRequest] Expect            100-continue
[HttpRequest] Host              **************
[HttpRequest] X-FECTransaction  63dc8855-e55d-48b0-b31b-ec44ea52b56e
[root] [root] type              object
[root] [codeNo] type            number
[root] codeNo                   100
[root] [typeNo] type            number
[root] typeNo                   1
[root] [relatedPkey] type       number
[root] relatedPkey              269222839
[root] [info] type              string
[root] info                     something
[root] [createTime] type        string
[root] createTime               /Date(1416002315864)/
[root] [dict] type              object
[root] [one] type               string 
[root] one                      first 
[root] [two] type               string
[root] two                      second 
[root] [three] type             string 
[root] three                    third

提供此信息供将来参考:根据以下段落(从),WCF似乎无法直接反序列化字典:

字典不是直接使用JSON的方法。 WCF中可能不以相同的方式支持字典 与使用其他JSON技术的预期一样。例如,如果 在字典中,“abc”映射到“xyz”,而“def”映射到42, JSON表示不是{“abc”:“xyz”,“def”:42},而是 [{“键”:“abc”,“值”:“xyz”},{“键”:“定义”,“值”:42}]


我确认.Serialization.JavaScriptSerializer可以很好地处理字典。但是WCF中的反序列化程序没有。不知道为什么——或者为什么这个问题从来没有被解决过——但你在这里。我找到了几篇关于替代反序列化程序实现的文章,但选择不采用这种方式。

这是在DataContract序列化程序中完成的。也许你可以使用它: 添加到DataContract类的DataMember属性。然后对用于对象的预期类型使用KnownType属性。在以下示例中,我在集合中使用int或bool:

[DataContract]
    [KnownType(typeof(int[]))]
    [KnownType(typeof(bool[]))]
    public class CompositeType
    {
        [DataMember]
        public object UsedForKnownTypeSerializationObject;

    [DataMember]
    public string StringValue
    {
        get;
        set;
    }
    [DataMember]
    public Dictionary<string, object> Parameters
    {
        get;
        set;
    }
}
[DataContract]
[KnownType(typeof(int[]))]
[KnownType(typeof(bool[]))]
公共类复合类型
{
[数据成员]
用于WorkNownTypeSerializationObject的公共对象;
[数据成员]
公共字符串字符串值
{
得到;
设置
}
[数据成员]
公共字典参数
{
得到;
设置
}
}

从您的回复中,我发现这个页面似乎支持您的建议。我现在没有时间来测试这个,但它似乎是一个有效的方法。有鉴于此,我会把你的答案记下来,直到有人证明不是这样。
[HttpRequest] Method            POST
[HttpRequest] Content-Length    161
[HttpRequest] Content-Type      application/json; charset=utf-8
[HttpRequest] Authorization     Basic ZHNcYmFybmVzcjpSZWN0MWYxZXI=
[HttpRequest] Expect            100-continue
[HttpRequest] Host              **************
[HttpRequest] X-FECTransaction  63dc8855-e55d-48b0-b31b-ec44ea52b56e
[root] [root] type              object
[root] [codeNo] type            number
[root] codeNo                   100
[root] [typeNo] type            number
[root] typeNo                   1
[root] [relatedPkey] type       number
[root] relatedPkey              269222839
[root] [info] type              string
[root] info                     something
[root] [createTime] type        string
[root] createTime               /Date(1416002315864)/
[root] [dict] type              object
[root] [one] type               string 
[root] one                      first 
[root] [two] type               string
[root] two                      second 
[root] [three] type             string 
[root] three                    third
[DataContract]
    [KnownType(typeof(int[]))]
    [KnownType(typeof(bool[]))]
    public class CompositeType
    {
        [DataMember]
        public object UsedForKnownTypeSerializationObject;

    [DataMember]
    public string StringValue
    {
        get;
        set;
    }
    [DataMember]
    public Dictionary<string, object> Parameters
    {
        get;
        set;
    }
}