Encapsulate values returned by JSON within a single object

Asked

Viewed 1,456 times

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?

  • 3

    'Cause you need me to come back like that?

  • Exactly why do you need this return? Wouldn’t it be better to create this client-side variable and assign the value to it?

  • return this.Json(new { licencas = licencas }, JsonRequestBehavior.AllowGet); no? or return this.Json(new { licencas = licencas.ToArray()}, JsonRequestBehavior.AllowGet);

  • 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 = '

3 answers

2


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

It just doesn’t. You cannot return a JSON this way because the JSON syntax does not allow, the format most suitable for your object is for sure:

{

    "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 code c# to return this JSON is the one informed by @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:

json-parser-fr

Later you can access it using:

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

0

You will need to create a new ActionResultto do this. I have done in the past a JsonVariableResult who did what you wanted, but with the and news in Framework ended up deleting the code. So as not to frustrate you, lower the ILSPY, and take a look at the source of ActionResults, they are very simple, create yours and be happy!

0

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

return this.Json(new { licencas }, JsonRequestBehavior.AllowGet);
  • Miguel, the above codex returns: {"licencas":[{"Responsavel":"José","Ticket":79007,"Descricao":"RC - 01 Desktop standard - José","Status":"Pending Management","SLA":"10/10/2013"},{"Responsavel":"Maria","Ticket":79037,"Descricao":"RC - 01 Standard Notebook - Maria","Status":"Pending Request","SLA":"10/11/2013"}]} com { starting and "licenses" as string and :

  • 1

    You should know that the value exactly the way you want it, is not a valid JSON. No json includes an equal sign.

  • That’s why I asked why it needed to be that way, because if you take the JSON result and assign it to a variable licencas will be the closest way @Jhonatan wants...(Ex: success: function(data) { var licencas = data; // ... o q quiser } )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.