JSON Handling with Asp.net Web API

0

Hello, I am putting together an educational application where I have a course calendar. In this project I'm using a jquery calendar, and it uses json to mount dates that have scheduled event.

The plugin ( w3widgets.com/responsive-calendar/ ) uses the following json to mount events in the calendar: / p>

"2014-12-22": { "url" : "link curso" },
"2014-12-26": { "url" : "url do curso"},
"2015-01-12": { "url" : "link deste curso"}

Using my api web application, I was able to generate the following json

{
    "data": "29-12-2014",
    "url": "gestao-agropecuaria"
},
{
    "data": "26-12-2014",
    "url": "producao-animal"
},
{
    "data": "06-01-2015",
    "url": "teste"
}

Below the actionResult method and the method where the query mysql is created for query in the database

public IHttpActionResult Get()
{
    var events = new Admin.AppClass.Agenda();
    return Ok(events.ListarDatasCalendario());
}



public DataTable ListarDatasCalendario()
{
            Mysql mysql = new Mysql();

            try
            {

                return mysql.ExecutarComando(@" SELECT DATE_FORMAT(a.data, '%d-%m-%Y') as data, 
                                                               c.nome as url 
                                                          FROM agenda a, cursos c
                                                         WHERE a.curso = c.id
                                                         ORDER BY data DESC");

            }
            catch (Exception)
            {
                throw;
            }
}

Can anyone help me with how I can configure json in an acceptable way to work in pluggin?

    
asked by anonymous 22.12.2014 / 18:41

1 answer

2

I'll show you how to return a list with the expected json, try converting your DataTable to fill this dictionary:

public Dictionary<string, object> ListarDatasCalendario()
{
    var lista = new Dictionary<string, object>();

    lista.Add("2014-01-01", new { url = "url 1" });
    lista.Add("2014-01-02", new { url = "url 2" });
    lista.Add("2014-01-03", new { url = "url 3" });

    return lista;
}
    
22.12.2014 / 19:03