5
I have a question, I have this code below on controller
:
[Authorize]
public JsonResult Teste()
{
var licencas = new List<Object>();
licencas.Add(new {
Responsavel = "José",
Ticket = 79007,
Descricao = "RC - 01 Desktop padrão - José",
Status = "Pendente Gestão",
SLA = "10/10/2013"
});
licencas.Add(new {
Responsavel = "Maria",
Ticket = 79037,
Descricao = "RC - 01 Notebook padrão - Maria",
Status = "Pendente Pedido",
SLA = "10/11/2013"
});
return this.Json(licencas, JsonRequestBehavior.AllowGet);
}
Which returns the following JSON:
[
{
"Responsavel":"José",
"Ticket":79007,"Descricao":"RC - 01 Desktop padrão - José",
"Status":"Pendente Gestão",
"SLA":"10/10/2013"
},
{"Responsavel":"Maria",
"Ticket":79037,
"Descricao":"RC - 01 Notebook padrão - Maria",
"Status":"Pendente Pedido",
"SLA":"10/11/2013"
}
]
But I need you to come back like this:
licencas = [
{
"Responsavel":"José",
"Ticket":79007,"Descricao":"RC - 01 Desktop padrão - José",
"Status":"Pendente Gestão",
"SLA":"10/10/2013"
},
{"Responsavel":"Maria",
"Ticket":79037,
"Descricao":"RC - 01 Notebook padrão - Maria",
"Status":"Pendente Pedido",
"SLA":"10/11/2013"
}
]
What do I do to return JSON this way?
'Cause you need me to come back like that?
– Tafarel Chicotti
Exactly why do you need this return? Wouldn’t it be better to create this client-side variable and assign the value to it?
– Rodrigo Santos
return this.Json(new { licencas = licencas }, JsonRequestBehavior.AllowGet);
no? orreturn this.Json(new { licencas = licencas.ToArray()}, JsonRequestBehavior.AllowGet);
– user3628
It’s weird, there must already be something out of the ordinary in your code. So an attempt would be to return the way @Miguelangelo demonstrated and do a post-processing before using, make sure that JSON is in string format and use a naughty replace of '"licencas":' for 'licencas = '
– iuristona