C# 模型不支持反序列化
我在尝试解码Json对象时遇到此错误 错误:数组反序列化不支持键入“realstate.Models.PesquisaModel.pesquisaClienteListModel” 这是字符串: JSON对象:C# 模型不支持反序列化,c#,json,asp.net-mvc-4,C#,Json,Asp.net Mvc 4,我在尝试解码Json对象时遇到此错误 错误:数组反序列化不支持键入“realstate.Models.PesquisaModel.pesquisaClienteListModel” 这是字符串: JSON对象: jsonObj=[{ "idCliente":"2", "nome":"Guilherme Longo", "email":"guilhermelongo@outlook.com.br", "tipoPessoa":"1", "observacao":"Mais conteúdo", "
jsonObj=[{
"idCliente":"2",
"nome":"Guilherme Longo",
"email":"guilhermelongo@outlook.com.br",
"tipoPessoa":"1",
"observacao":"Mais conteúdo",
"rg":"435180307",
"cpf":"341.307.948-41",
"cnpj":null,
"estado":"SP",
"cidade":"Ribeirão Preto",
"logradouro":"Rua",
"endereco":"Brigadeiro Tobias de Aguiar",
"numero":"469",
"bairro":"Independência",
"complemento":"Bloco A"
},
{"idCliente":"8",
"nome":"Guilherme Longo",
...
}]
下面是我面对这个错误的地方:
public ActionResult dataSetClientes(string jsonObj)
{
PesquisaModel.pesquisaClienteListModel items = new JavaScriptSerializer().Deserialize<PesquisaModel.pesquisaClienteListModel>(jsonObj);
...
}
publicActionResult数据集客户端(字符串jsonObj)
{
PesquisaModel.pesquisaClienteListModel items=新的JavaScriptSerializer()。反序列化(jsonObj);
...
}
编辑1:
jsonObj=[{
"idCliente":"2",
"nome":"Guilherme Longo",
"email":"guilhermelongo@outlook.com.br",
"tipoPessoa":"1",
"observacao":"Mais conteúdo",
"rg":"435180307",
"cpf":"341.307.948-41",
"cnpj":null,
"estado":"SP",
"cidade":"Ribeirão Preto",
"logradouro":"Rua",
"endereco":"Brigadeiro Tobias de Aguiar",
"numero":"469",
"bairro":"Independência",
"complemento":"Bloco A"
},
{"idCliente":"8",
"nome":"Guilherme Longo",
...
}]
这就是模型:
public class PesquisaModel
{
public class pesquisaClienteModel
{
public string idCliente { get; set; }
public string nome { get; set; }
public string email { get; set; }
public string tipoPessoa { get; set; }
public string observacao { get; set; }
public string rg { get; set; }
public string cpf { get; set; }
public string cnpj { get; set; }
public string estado { get; set; }
public string cidade { get; set; }
public string logradouro { get; set; }
public string endereco { get; set; }
public string numero { get; set; }
public string bairro { get; set; }
public string complemento { get; set; }
}
public class pesquisaClienteListModel
{
public List<pesquisaClienteModel> item { get; set; }
}
}
公共类PesquisaModel
{
公共类PesquisaClientModel
{
公共字符串idCliente{get;set;}
公共字符串nome{get;set;}
公共字符串电子邮件{get;set;}
公共字符串tipoPessoa{get;set;}
公共字符串observaco{get;set;}
公共字符串rg{get;set;}
公共字符串cpf{get;set;}
公共字符串cnpj{get;set;}
公共字符串estado{get;set;}
公共字符串ciade{get;set;}
公共字符串logradouro{get;set;}
公共字符串endereco{get;set;}
公共字符串numero{get;set;}
公共字符串bairro{get;set;}
公共字符串补码{get;set;}
}
公共类pesquisaClienteListModel
{
公共列表项{get;set;}
}
}
您应该对ajax函数中的内容进行字符串化,以使其正常工作。这样做:
data = JSON.stringfy(/*myDataToSend*/)
您不必进行序列化,MVC模型绑定器将为您进行序列化。将动作参数更改为自定义类型
public ActionResult dataSetClientes(PesquisaModel jsonObj)
{
}
仅当类实现IEnumerable时,才能从json数组反序列化该类<代码>PesquisaModel.pesquisaClienteListModel没有。相反,请使用以下命令:
List<PesquisaModel> list = new JavaScriptSerializer().Deserialize<List<PesquisaModel>>(jsonObj);
PesquisaModel.pesquisaClienteListModel items = new PesquisaModel.pesquisaClienteListModel() { item = list };
List List=newJavaScriptSerializer()。反序列化(jsonObj);
PesquisaModel.pesquisaClienteListModel items=新的PesquisaModel.pesquisaClienteListModel(){item=list};
很抱歉这个糟糕的问题。刚刚修复。您可以调试它并将jsonObj字符串的内容发布到控制器端吗?您是否传递了数据:JSON.stringfy(/*myDataToSend*/)?@Fals谢谢您的回复。我没有将JSON对象转换为字符串。在使用JSON.stringfy(/*myDataToSend*/)之后,我能够在控制器中获取发布的数据。请发一张awnser,这样我可以接受。@Guillermelongo不客气!我贴的是awnsear!不知道这是否有助于原始海报,但修复了我的问题,干杯:)