0

How to receive object json with Web Api 2. I send the object correctly, but in the backend my object is null. See below.

My object JSON

This is my JSON object that will be sent in the request.

{
 "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"
 }
 ]
}

My object.

This is my mapped object. A class that needs to be populated with the JSON object that is received in the request.

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; }
}

My method in the WebAPi 2

[Route("CalcularRateioManual")]
[HttpPost]
public RespostaPadrao<bool> CalcularRateioManual([FromBody] RateioSimplesRequestModel parametro) // THIS OBJECT
{
 RespostaPadrao<bool> retorno = new RespostaPadrao<bool>();
 return retorno;
}

See the object isn't null, but the children objects is not completed

How I can this be perfectly?

asked Jan 12, 2018 at 14:29

3 Answers 3

3

Your lists are not public and json.net by default only maps public properties. Also a collection should not be public settable.

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>();
 }
}
answered Jan 12, 2018 at 14:34

1 Comment

That was it! Something so simple but of such importance. Thank you so much for helping me, my friend. You were brilliant. Hugs.
1

Please note that you are having list in the property of a class... json object hence needs to be formatted correctly and parametro should be sent instead of 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"
 }
 ];
peinearydevelopment
11.5k5 gold badges52 silver badges82 bronze badges
answered Jan 12, 2018 at 15:00

Comments

0

as mentioned by Oliver, making the Lists public should allow this to be converted to an object.

If you are using C#, one way to verify if the object can be serialized is to construct the object and use JsonConvert to convert it to the JSON equivalent. This should highlight any member protection level issues and also generate the JSON that's expected.

Based on the classes above, this generates the following 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": []
}
answered Jan 12, 2018 at 15:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.