Json format in return

3

I need to pass a json in the following format:

{rows:[
    { 
     data:[
       "1",
       "Nome teste",
       "Descricao teste"]
}]}

But when using jsonresult in my action, I do not know how to return that way

 public JsonResult Data()
    {
      var grupos= grupoService.BuscaTodos();

      return Json(new
      {
        rows = new
        {
          data = grupos
        }
      }, JsonRequestBehavior.AllowGet);
    }

It ends up like this:

{"rows":{
      "data":[{
             "Nome":"nome",
              "Descricao":"descricao",
              "Id":110,
              "DataCadastro":null}]
}}

Does anyone know what I need to do?

Blue is how I want it, red is how it's coming

    
asked by anonymous 20.08.2014 / 15:44

2 answers

4

Basically your layout would look like this:

return Json(new
        {
            rows = new object[] {
                new {
                    data = new string[]
                    {
                        "1", "NomeTeste", "Descricao Teste"
                    }
                }
            }
        }, JsonRequestBehavior.AllowGet);

If it's a list, for example:

var dados = new object[] 
{
    new object[] {"1", "NomeTeste 1", "Descricao Teste 1" },
    new object[] {"2", "NomeTeste 2", "Descricao Teste 2" }
};

return Json(new
{
    rows = new object[] {
        new {
            data =  dados
        }
    }
}, JsonRequestBehavior.AllowGet);

With a class:

public class Exemplo
{
    public int Id { get; set; }
    public String Nome { get; set; }
    public String Descricao { get; set; }
    public DateTime? DataCadatro { get; set; }
}   
IList<Exemplo> dados = new List<Exemplo>();
dados.Add(new Exemplo()
{
    Id = 1,
    Nome = "NomeTeste 1",
    Descricao = "Descricao 1"
});
dados.Add(new Exemplo()
{
    Id = 2,
    Nome = "NomeTeste 2",
    Descricao = "Descricao 2"
}); 
return Json(new
    {
        rows = new object[] {
            new {
                data =  dados.Select(x => new object[]{
                    x.Id.ToString(), x.Nome.ToString(), x.Descricao.ToString()
                })
            }
        }
    }, JsonRequestBehavior.AllowGet);

Forcingonlythefirstiteminthelist:

returnJson(new{rows=newobject[]{new{data=dados.Select(x=>newobject[]{x.Id,x.Nome.ToString(),x.Descricao.ToString()}).FirstOrDefault()}}},JsonRequestBehavior.AllowGet);

With New Edition the layout became clearer:

IList<Exemplo> dados = new List<Exemplo>();
dados.Add(new Exemplo()
{
    Id = 1,
    Nome = "NomeTeste 1",
    Descricao = "Descricao 1"
});
dados.Add(new Exemplo()
{
    Id = 2,
    Nome = "NomeTeste 2",
    Descricao = "Descricao 2"
});

return Json(new
{
    rows = dados.Select(x => new 
            {
                x.Id, 
                Data = new object[] { 
                    x.Id, x.Nome.ToString(), x.Descricao.ToString()
                }                            
            })                                    
}, JsonRequestBehavior.AllowGet);

    
20.08.2014 / 16:42
0

Try this:

    public JsonResult Data()
    {
        var grupos = grupoService.BuscaTodos().Select(data => new
        {
            data.Id,
            data.Nome,
            data.Descricao
        });
        return Json(new
        {
            rows = new
            {
                data = grupos
            }
        }, JsonRequestBehavior.AllowGet);
    }
    
20.08.2014 / 16:16