Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# JavaScriptDeseializer:无法序列化数组_C#_Asp.net_Json_Asp.net Ajax - Fatal编程技术网

C# JavaScriptDeseializer:无法序列化数组

C# JavaScriptDeseializer:无法序列化数组,c#,asp.net,json,asp.net-ajax,C#,Asp.net,Json,Asp.net Ajax,我的应用程序是asp.net。我必须将一些值发送回服务器。为此,我创建了一个对象,将其序列化并发送到服务器。在服务器上,我尝试将其反序列化 下面是我的代码 [Serializable] public class PassData { public PassData() { } public List<testWh> SelectedId { get; set; } publi

我的应用程序是asp.net。我必须将一些值发送回服务器。为此,我创建了一个对象,将其序列化并发送到服务器。在服务器上,我尝试将其反序列化 下面是我的代码

   [Serializable]
    public class PassData
    {
        public PassData()
        {  
        }

        public List<testWh> SelectedId { get; set; }

        public string SelectedControlClientId { get; set; }

        public string GroupTypeId { get; set; }

        public string SectionTypeId { get; set; }

  }


    [Serializable]
    public class testWh
    {
        public testWh()

        {
        }
        public string Id { get; set; }
    }


JavaScriptSerializer serializer = new JavaScriptSerializer();
//this can not serialize the SelectedId and the count remains 0
PassData data = serializer.Deserialize<PassData>(jsonString);
//this serialize in an anonymous object with key value pair
var data2 = serializer.DeserializeObject(textHiddenArguments.Text);
引号转义字符串

"{\"SelectedId\":{\"0\":\"ABCD\",\"1\":\"JKLM\"},\"SelectedControlClientId\":\"YTUTOOO\",\"GroupTypeId\":3,\"SectionTypeId\":\"1\"}"
我的问题是所选Id是testWH对象的数组。但是当我尝试去序列化它时,PassData的SelectedId属性(即list)不会被序列化,计数保持为零

我尝试使用数组而不是列表,它给出了一个异常“无参数构造函数…”


有人能解释一下我做错了什么吗?

这里的关键问题是JSON与您构建的对象不匹配。通过写入所需数据并序列化,可以看到这一点:

var obj = new PassData
{
    SelectedId = new List<testWh>
    {
        new testWh { Id = "ABCD"},
        new testWh { Id = "JKLM"}
    },
    GroupTypeId = "3",
    SectionTypeId = "1",
    SelectedControlClientId = "YTUTOOO"
};
string jsonString = serializer.Serialize(obj);
所以现在你需要决定你想要改变什么;JSON或类。以下替代类适用于原始JSON,例如:

public class PassData
{
    public Dictionary<string,string> SelectedId { get; set; }
    public string SelectedControlClientId { get; set; }
    public string GroupTypeId { get; set; }
    public string SectionTypeId { get; set; }
}
公共类PassData
{
公共字典SelectedId{get;set;}
公共字符串SelectedControlClientId{get;set;}
公共字符串GroupTypeId{get;set;}
公共字符串SectionTypeId{get;set;}
}

无法理解它。你所说的代码字段/字段初始值设定项是什么意思?啊,对不起,这只是我粘贴时更改内容的IDE插件
{"SelectedId":[{"Id":"ABCD"},{"Id":"JKLM"}],
 "SelectedControlClientId":"YTUTOOO","GroupTypeId":"3","SectionTypeId":"1"}
public class PassData
{
    public Dictionary<string,string> SelectedId { get; set; }
    public string SelectedControlClientId { get; set; }
    public string GroupTypeId { get; set; }
    public string SectionTypeId { get; set; }
}