C# 在WEB API 2中接收对象JSON
如何使用Web Api 2接收对象json。 我正确发送了对象,但在后端,我的对象为空。 见下文 我的对象JSON 这是我的JSON对象,将在请求中发送C# 在WEB API 2中接收对象JSON,c#,json,asp.net-web-api2,C#,Json,Asp.net Web Api2,如何使用Web Api 2接收对象json。 我正确发送了对象,但在后端,我的对象为空。 见下文 我的对象JSON 这是我的JSON对象,将在请求中发送 { "ItensRateio" : [ { "TipoColetorCusto" : "1", "ColetorCusto" : "MRBHAD", "Valor": "R$ 25.22", "Descricao": "Rat
{
"ItensRateio" : [
{
"TipoColetorCusto" : "1",
"ColetorCusto" : "MRBHAD",
"Valor": "R$ 25.22",
"Descricao": "Rateio do Brasil"
},
{
"TipoColetorCusto" : "1",
"ColetorCusto" : "MRBHAD",
"Valor": "R$ 25.22",
"Descricao": "Rateio do Brasil"
}
]
}
public class RateioSimplesRequestModel
{
List<ItemRateio> ItensRateio { get; set; }
List<ItemMaterial> ItensMaterial { get; set; }
public RateioSimplesRequestModel()
{
ItensRateio = new List<ItemRateio>();
ItensMaterial = new List<ItemMaterial>();
}
}
public class ItemRateio
{
public string TipoColetorCusto { get; set; }
public string ColetorCusto { get; set; }
public string Valor { get; set; }
public string Descricao { get; set; }
}
public class ItemMaterial
{
public string CNAE { get; set; }
public string CodigoMaterial { get; set; }
public string Descricao { get; set; }
}
我的目标
这是我的映射对象。需要使用请求中接收的JSON对象填充的类
{
"ItensRateio" : [
{
"TipoColetorCusto" : "1",
"ColetorCusto" : "MRBHAD",
"Valor": "R$ 25.22",
"Descricao": "Rateio do Brasil"
},
{
"TipoColetorCusto" : "1",
"ColetorCusto" : "MRBHAD",
"Valor": "R$ 25.22",
"Descricao": "Rateio do Brasil"
}
]
}
public class RateioSimplesRequestModel
{
List<ItemRateio> ItensRateio { get; set; }
List<ItemMaterial> ItensMaterial { get; set; }
public RateioSimplesRequestModel()
{
ItensRateio = new List<ItemRateio>();
ItensMaterial = new List<ItemMaterial>();
}
}
public class ItemRateio
{
public string TipoColetorCusto { get; set; }
public string ColetorCusto { get; set; }
public string Valor { get; set; }
public string Descricao { get; set; }
}
public class ItemMaterial
{
public string CNAE { get; set; }
public string CodigoMaterial { get; set; }
public string Descricao { get; set; }
}
公共类RateIOSimpleRequestModel
{
列出ItensRateio{get;set;}
列出ItensMaterial{get;set;}
公共费率IMPLESRequestModel()
{
ItensRateio=新列表();
ItensMaterial=新列表();
}
}
公共类项目费率
{
公共字符串TipoColetorCusto{get;set;}
公共字符串ColetorCusto{get;set;}
公共字符串值{get;set;}
公共字符串描述符{get;set;}
}
公共类项目材料
{
公共字符串CNAE{get;set;}
公共字符串{get;set;}
公共字符串描述符{get;set;}
}
我在WebAPi 2中的方法
[路径(“计算速率手动”)]
[HttpPost]
public RespostaPadrao CalcularRateiManual([FromBody]RateIOSimpleRequestModel Parameter)//此对象
{
RespostaPadrao returno=新RespostaPadrao();
返回号;
}
我怎么能做到完美呢?您的列表不是公共的,默认情况下json.net只映射公共属性。此外,集合不应是可公共设置的
public class RateioSimplesRequestModel
{
public List<ItemRateio> ItensRateio { get; private set; }
public List<ItemMaterial> ItensMaterial { get; private set; }
public RateioSimplesRequestModel()
{
ItensRateio = new List<ItemRateio>();
ItensMaterial = new List<ItemMaterial>();
}
}
公共类RateIOSimpleRequestModel
{
公共列表ItensRateio{get;private set;}
公共列表ItensMaterial{get;private set;}
公共费率IMPLESRequestModel()
{
ItensRateio=新列表();
ItensMaterial=新列表();
}
}
您的列表不是公共的,默认情况下json.net只映射公共属性。此外,集合不应是可公共设置的
public class RateioSimplesRequestModel
{
public List<ItemRateio> ItensRateio { get; private set; }
public List<ItemMaterial> ItensMaterial { get; private set; }
public RateioSimplesRequestModel()
{
ItensRateio = new List<ItemRateio>();
ItensMaterial = new List<ItemMaterial>();
}
}
公共类RateIOSimpleRequestModel
{
公共列表ItensRateio{get;private set;}
公共列表ItensMaterial{get;private set;}
公共费率IMPLESRequestModel()
{
ItensRateio=新列表();
ItensMaterial=新列表();
}
}
请注意,在类的属性中有列表
。。。因此,json对象需要正确格式化,并且应该发送parametro
,而不是ItensRateio
var parametro = {};
parametro.ItensRateio = [
{
"TipoColetorCusto" : "1",
"ColetorCusto" : "MRBHAD",
"Valor": "R$ 25.22",
"Descricao": "Rateio do Brasil"
},
{
"TipoColetorCusto" : "1",
"ColetorCusto" : "MRBHAD",
"Valor": "R$ 25.22",
"Descricao": "Rateio do Brasil"
}
];
请注意,您在类的属性中有列表
。。。因此,json对象需要正确格式化,并且应该发送parametro
,而不是ItensRateio
var parametro = {};
parametro.ItensRateio = [
{
"TipoColetorCusto" : "1",
"ColetorCusto" : "MRBHAD",
"Valor": "R$ 25.22",
"Descricao": "Rateio do Brasil"
},
{
"TipoColetorCusto" : "1",
"ColetorCusto" : "MRBHAD",
"Valor": "R$ 25.22",
"Descricao": "Rateio do Brasil"
}
];
正如Oliver所提到的,公开列表应该允许将其转换为对象
如果使用C#,验证对象是否可以序列化的一种方法是构造对象并使用JsonConvert将其转换为JSON等价物。这将突出显示任何成员保护级别问题,并生成预期的JSON
基于上述类,这将生成以下JSON:
{
"ItensRateio": [
{
"TipoColetorCusto": "1",
"ColetorCusto": "MRBHAD",
"Valor": "R$ 25.22",
"Descricao": "Rateio do Brasil"
},
{
"TipoColetorCusto": "1",
"ColetorCusto": "MRBHAD",
"Valor": "R$ 25.22",
"Descricao": "Rateio do Brasil"
}
],
"ItensMaterial": []
}
正如Oliver所提到的,公开列表应该允许将其转换为对象
如果使用C#,验证对象是否可以序列化的一种方法是构造对象并使用JsonConvert将其转换为JSON等价物。这将突出显示任何成员保护级别问题,并生成预期的JSON
基于上述类,这将生成以下JSON:
{
"ItensRateio": [
{
"TipoColetorCusto": "1",
"ColetorCusto": "MRBHAD",
"Valor": "R$ 25.22",
"Descricao": "Rateio do Brasil"
},
{
"TipoColetorCusto": "1",
"ColetorCusto": "MRBHAD",
"Valor": "R$ 25.22",
"Descricao": "Rateio do Brasil"
}
],
"ItensMaterial": []
}
就这样!如此简单但如此重要的事情。非常感谢你帮助我,我的朋友。你很聪明。拥抱,就是这样!如此简单但如此重要的事情。非常感谢你帮助我,我的朋友。你很聪明。拥抱。