C#包含列表<>对象为Json结果

时间:2016-10-16 14:42:48

标签: c# json list

我有这个型号:

议程

 public class Agenda
    {
        [Key]
        public int AgendaID { get; set; }
        public DateTime data { get; set; }
        public List<Agendamento> agentamentos { get; set; }
        public string status { get; set; }
    }

Agendamento

public class Agendamento
{
    [Key]
    public int AgendamentoID { get; set; }
    public Clientes cliente { get; set; }
    public DateTime data { get; set; }
    public string botijao { get; set; }
    public int quantidade { get; set; }
    public string veiculo { get; set; }
    public string status { get; set; }
}

我的DAO:

AgendaDAO

 public class AgendaDAO
{
    private ApplicationDbContext db = new ApplicationDbContext();

    public List<Agenda> Listar()
    {
        return db.Agenda.Include(a => a.agentamentos).ToList();
    }
}

AgendamentoDAO

public class AgendamentoDAO
{
    private ApplicationDbContext db = new ApplicationDbContext();

    public List<Agendamento> Listar()
    {
        return db.Agendamentoes.Include(a => a.cliente).ToList();
    }
}

在我的ApiController中,我有这样的方法:

public class ApiController : Controller
{
    // GET: Api/ListarAgenda
    public ActionResult ListarAgenda()
    {
        AgendaDAO adao = new AgendaDAO();

        return Json(adao.Listar(), JsonRequestBehavior.AllowGet);
    }

    // GET: Api/ListarAgendamento
    public ActionResult ListarAgendamento()
    {
        AgendamentoDAO adao = new AgendamentoDAO();

        return Json(adao.Listar(), JsonRequestBehavior.AllowGet);
    }
}

我的Json结果:

阿比/ ListarAgenda

    [
  {
    "AgendaID": 13,
    "data": "/Date(1476327600000)/",
    "agentamentos": [],
    "status": null
  },
  {
    "AgendaID": 14,
    "data": "/Date(1476669600000)/",
    "agentamentos": [],
    "status": null
  }
]

阿比/ ListarAgendamento

    [
      {
        "AgendamentoID": 72,
        "cliente": {
      "ClientesID": 1007,
      "Nome": "Maria Zilda",
      "CPF": "00124794321",
      "Telefone": 30198269,
      "Estado": "PR",
      "Cidade": "Curitiba",
      "Rua": "Altevir De Souza Gonçalves",
      "Numero": 59,
      "Lat": "-25.3955309",
      "Long": "-49.2168943"
    },
    "data": "/Date(1476327600000)/",
    "botijao": "P13",
    "quantidade": 1,
    "veiculo": "VW Saveiro ",
    "status": null
  }]

我的问题是:我需要在Api / ListarAgenda的json结果中查看具有clientes属性的议程列表。像这样:

[
  {
    "AgendaID": 13,
    "data": "/Date(1476327600000)/",
    "agentamentos": [
      {
        "AgendamentoID": 72,
        "cliente": {
           "ClientesID": 1007,
           "Nome": "Maria Zilda",
           "CPF": "00124794321",
           "Telefone": 30198269,
           "Estado": "PR",
           "Cidade": "Curitiba",
           "Rua": "Altevir De Souza Gonçalves",
           "Numero": 59,
           "Lat": "-25.3955309",
           "Long": "-49.2168943"
          },
         "data": "/Date(1476327600000)/",
         "botijao": "P13",
         "quantidade": 1,
         "veiculo": "VW Saveiro ",
         "status": null
      }],
    "status": null
  }
]

&#34;包含&#34;不是很好。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

您正在使用急切加载吗?您需要在每个加载的议程中包含客户