0

I want to convert my data to json. I aslo want to use JSON.NET, for I am working on ASP.net MVC. I have followed those two links Error during serialization using the JSON JavaScriptSerializer. and Fastest JSON Serializer for .NET released, but I am not able yet to do it. I have an action which calls a method of a class. This method must return the JSON. So first of all how to serialize the data and what type of return should it be. This is my code snippet for the action first then the Json class and method.

Most Importantly is that I don't want the Json as a string because I want to be able to access the fields in it using the "."

public ActionResult HighChartAction(int id)
{
   JsonModel j = new JsonModel();
   ??? chartData = j.GetMessagesforChart(id); // What should be the type of chartData for JSON data
}
----------------------    
public class JsonModel
{
    public JsonResult GetMessagesforChart(int id)
    {
          DataRepository _messageRepository = new DataRepository();
          var gluc = _messageRepository.GetAllMessages(id);
          var json = JsonConvert.SerializeObject(gluc); // THIS IS A STRING
          return json; // ERROR BECAUSE I WANT IT A JSONRESULT NOT STRING
     }
}

---------------------
    namespace MonitorUI.Models
    {
        public class DataBase
        {
            public int id { get; set; }

            public DateTime date_time { get; set; }

            public int my_value{ get; set; }
        }
    }

so var gluc is of type IEnumerable(DataBase)


Related continuing question here: How to fill database list in series and xAxis objects of HighChart

Help Please

Community
  • 1
  • 1
user2217303
  • 356
  • 4
  • 16

2 Answers2

1

Your function :

public JsonResult GetMessagesforChart(int id)
{
      DataRepository _messageRepository = new DataRepository();
      var gluc = _messageRepository.GetAllMessages(id);
      var json = JsonConvert.SerializeObject(gluc); // THIS IS A STRING
      return json; // ERROR BECAUSE I WANT IT A JSONRESULT NOT STRING
 }

Update with this function :

public JsonResult GetMessagesforChart(int id)
{
      DataRepository _messageRepository = new DataRepository();
      DataBase gluc = _messageRepository.GetAllMessages(id);
      return JsonConvert.SerializeObject(gluc);
 }
Krunal Mevada
  • 1,637
  • 1
  • 17
  • 28
0

To return JsonResult you can simply wrap the object with Json() like so

public JsonResult GetMessagesforChart(int id)
{
    DataRepository _messageRepository = new DataRepository();
    var gluc = _messageRepository.GetAllMessages(id);
    return Json(gluc);
}

Json is about serialization so the format here is string, if you need to access field with ".", you need to deserialize it to object

uowzd01
  • 1,015
  • 10
  • 19
  • But I already have it as an object check please my update of gluc variable type.. If this is the case then what is the benefit of Json? My aim is to get data in form of json to add then to highchart – user2217303 Apr 29 '15 at 07:17
  • json is used mostly when doing ajax calls, or constrained by api. If you just need to pass model from controller to the view, you don't need json, you can pass ienumerable – uowzd01 Apr 29 '15 at 07:20
  • I have used the ienumerable type and converted it to a list.. There is a code assigning hardcode values I want to replace them by my list/arrays. It is not working.. I will place the code at the end of the question. Please help @uowzd01 – user2217303 Apr 29 '15 at 08:00