0
I have a code that generates me a Json file in this format:
[
{
"$id": "1",
"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
}
]
I’d like to write the exit this way:
{
"venda":
[
{
"$id": "1",
"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
}
]
}
This is the code:
[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, listar.ToArray());
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
}
}
If the code is changed to :
return Request.CreateResponse(HttpStatusCode.OK, new { jogo = listar.ToArray() });
the return stays like this:
{
"$id": "1",
"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
}
]
}
very valid help, see that the $id of Json repeats and changes the number, is correct like this or has another way to do?
– Harry
updated the response
– Felipe Assunção
I appreciate the help, but my idea was to remove this $id that appears before sale : in my question see how it looks right at the end of it
– Harry
add the following had on your
webapi.config.cs
: var json = config.Formatters.Jsonformatter; json.SerializerSettings.Preservereferenceshandling = Newtonsoft.Json.Preservereferenceshandling.None;– Felipe Assunção
Great answer! Immensely grateful!
– Harry