Change in json file format

2

My file is coming in this format, but I wish it did not appear this #id before sale:

{
  "$id": "1", //como não mostra isso?
  "venda": [
    {
      "$id": "2",
      "poule": 73,
      "idusuario": 4,
      "vendedor": "ITAMAR SOUZA",
      "total": 50.00,
      "datajogo": "2016-01-19T00:00:00",
      "terminal": "(11)985590116",
      "empresa": "SANTIAGO - LOJA 01",
      "nsu": 73
    }
  ]
}

Code that is generating Json:

        [HttpGet]
        [Route("consulta/ListarUltimoJogosRalizado/{idusuario}")]
        public HttpResponseMessage ListarTodosJogosAtivos(int idusuario)
        {
            try
            {
                var tTabela = new  JogoAplicacao();
                var listar = tTabela.ListarPoId(idusuario);
                return Request.CreateResponse( HttpStatusCode.OK,  new { venda = listar.ToArray() } );
            }
            catch (Exception ex)
            {

                return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
            }
        }
    
asked by anonymous 22.01.2016 / 19:38

2 answers

1

Add the code snippet below in your WebApiConfig.cs file:

var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
    
22.01.2016 / 19:44
-1

There is a widely used method of using ViewModels.

You create a ViewModel for each custom return of your API actions.

Ex:

public class VendaViewModel
{
    public int Id { get; set; }
    public int Poule { get; set; }
    public int IdUsuario { get; set; }
    public string Vendedor { get; set; }
    public decimal Total { get; set; }
    public DateTime DataJogo { get; set; }
    public string Terminal { get; set; }
    public string Empresa { get; set; }
    public int NSU { get; set; }

    public VendaViewModel ToViewModel(Venda venda)
    {
        // Recomendo refatorar com uso de AutoMapper: http://automapper.org/
        return new VendaViewModel 
        {
            Id = venda.id,
            Poule = venda.Poule,
            IdUsuario = venda.IdUsuario,            
            Vendedor = venda.Vendedor,
            Total = venda.Total,
            DataJogo = venda.DataJogo,
            Terminal = venda.Terminal,
            Empresa = venda.Empresa,
            NSU = venda.NSU
        };
    }
}

And in your action:

// ...
var listar = tTabela.ListarPoId(idusuario);
return lista.Select(Venda.ToViewModel);

In this way, you can customize what will actually be delivered to the client. You can even filter out fields that are not interesting, reducing traffic, and also formatting output, for example, from Total, using regional settings.

    
22.01.2016 / 19:58