[英]How to serialize my data that are stored in C# dictionary into json object by using C# and json.net?
[英]How can I serialize my data using Json.net?
我想將數據轉換為json。 我也想使用JSON.NET,因為我正在使用ASP.net MVC。 我在使用JSON JavaScriptSerializer進行序列化時遵循了這兩個鏈接錯誤。 並發布了適用於.NET的最快的JSON序列化程序 ,但我還無法做到這一點。 我有一個調用類方法的操作。 此方法必須返回JSON。 因此,首先如何序列化數據以及應該返回哪種類型的數據。 這是我的動作的代碼片段,然后是Json類和方法。
最重要的是,我不想將Json作為字符串,因為我希望能夠使用“。”來訪問其中的字段。
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; }
}
}
因此var gluc的類型為IEnumerable(DataBase)
這里相關的繼續存在的問題: 如何在HighChart的系列和xAxis對象中填充數據庫列表
請幫助
您的職能:
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
}
使用此功能更新:
public JsonResult GetMessagesforChart(int id)
{
DataRepository _messageRepository = new DataRepository();
DataBase gluc = _messageRepository.GetAllMessages(id);
return JsonConvert.SerializeObject(gluc);
}
要返回JsonResult,您可以像這樣簡單地用Json()包裝對象
public JsonResult GetMessagesforChart(int id)
{
DataRepository _messageRepository = new DataRepository();
var gluc = _messageRepository.GetAllMessages(id);
return Json(gluc);
}
Json與序列化有關,因此此處的格式為字符串,如果您需要使用“。”訪問字段,則需要將其反序列化為對象
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.