Handling JSON with Asp.net Web API

Asked

Viewed 251 times

0

Hello, I’m setting up an educational application where I have a course schedule. In this project I am using a jquery calendar, and this one uses a json to assemble the dates that has scheduled event.

The pluggin (w3widgets.com/Responsive-Calendar/) uses the following json to mount events in the calendar:

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

Using my web api 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 is the actionResult method and the method where the mysql query for database query is created

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 assist me on how I can set up my json in an acceptable way to work on pluggin?

1 answer

2


I will demonstrate how to return a list with the expected json, try to convert 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;
}
  • Thanks @iuristona, thanks man, it all worked out. Hugs

Browser other questions tagged

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