Encapsulate values returned by JSON within a single object

5

I have a question, I have this code below in 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);

    }

That 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 it to return 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?

    
asked by anonymous 18.03.2014 / 19:28

3 answers

2

Answer your question "How do I return JSON in this way?"

It just does not. You can not return a JSON in this way because the JSON syntax does not allow, the format most suitable to your object for sure is:

{

    "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":"R‌​C - 01 Notebook padrão - Maria",
            "Status":"Pendente Pedido",
            "SLA":"10/11/2013"
        }
    ]

}

The C # code to return this JSON is the @MiguelAngelo :

return this.Json(new { licencas }, JsonRequestBehavior.AllowGet);

You can better understand JSON using sites like this at a glance in your JSON for example:

Lateryouwillbeabletoaccessitusing:

seuJSON.licensas[0].Responsavel; // José seuJSON.licensas[0].Ticket; // 79007 seuJSON.licensas[0].Descricao; // RC - 01 Desktop padrão - José seuJSON.licensas[0].Status; // Pendente Gestão seuJSON.licensas[0].SLA; // 10/10/2013 seuJSON.licensas[1].Responsavel; // Maria seuJSON.licensas[1].Ticket; // 79037 seuJSON.licensas[1].Descricao; // RC - 01 Notebook padrão - Maria seuJSON.licensas[1].Status; // Pendente Pedido seuJSON.licensas[1].SLA; // 10/11/2013     
20.03.2014 / 12:31
0

Do so to return json that will work out the way you want it:

return this.Json(new { licencas }, JsonRequestBehavior.AllowGet);
    
18.03.2014 / 19:32
0

You will need to create a new ActionResult to do this. I've done in the past a JsonVariableResult that did what you wanted, but with the and news in the Framework I deleted the code. So as not to be frustrated, download the ILSPY , and take a look at the source of ActionResults , they are very simple, create yours and be happy !

    
20.03.2014 / 07:27