Thiago Henrique
Thiago Henrique

Reputation: 390

Receive object JSON in the WEB API 2

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?

Upvotes: 0

Views: 257

Answers (3)

singleton
singleton

Reputation: 171

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": []
}

Upvotes: 0

Wahaj A Khan
Wahaj A Khan

Reputation: 111

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

Upvotes: 1

Oliver
Oliver

Reputation: 45101

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

Upvotes: 3

Related Questions