【问题标题】:deserialize object that contains list of int as property反序列化包含 int 列表作为属性的对象
【发布时间】:2014-02-26 17:09:21
【问题描述】:

我正在尝试使用 Newtonsoft.Json.JsonConvert 将 Json 反序列化到我的对象中。 我的json是:{"SelectedContentsID[]":"31807,32493,39517","pageSize":"20","SisconContentSubDialogEnum":"0","searchCriteria":"","pageIndex":"1"}

相应的类是:

[DataContract]
public class ContentGetHandlerDTO : ListBaseHandlerDto
{
    [DataMember(Name = "SelectedCourseId")]
    public int SelectedCourseId { get; set; }
    [DataMember]
    public System.Collections.Generic.List<int> SelectedContentsID { get; set; }
    public ContentGetHandlerDTO() {
        this.SelectedContentsID = new System.Collections.Generic.List<int>();
    }
}

ListBaseHandlerDto 只是一个包含一些公共属性的类。 问题是,反序列化方法只是忽略了 int 的列表并带来了一个空列表。

【问题讨论】:

    标签: json serialization json.net


    【解决方案1】:

    您的 Json 看起来不正确,应该是这样的:

    {
    "SelectedContentsID":[31807,32493,39517],
    "pageSize":"20",
    "SisconContentSubDialogEnum":"0",
    "searchCriteria":"",
    "pageIndex":"1"
    }
    

    具体查看您的 SelectedContentsID, 你有:

    "SelectedContentsID[]":"31807,32493,39517"
    

    当一个 json int 数组应该是这样的:

    "SelectedContentsID":[31807,32493,39517]
    

    如果您感到困惑,这里是一个很好的参考 - http://www.w3schools.com/json/json_syntax.asp

    【讨论】:

    • 感谢 Zach,这是由于使用 JavaScriptSerializer 从 Dictionary 到 Json 的序列化而发生的。现在,我m facing other problem: the Deserializer is throwing: "Could not cast or convert from System.String to System.Collections.Generic.List1[System.Int32]。”
    • 如果这是正确答案,请点击复选标记。
    • 我很乐意提供帮助,但您需要将此标记为正确答案,并开始一个新问题,因为问题现在不同了。 :)
    猜你喜欢
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 2017-07-21
    • 1970-01-01
    相关资源
    最近更新 更多