将字典对象转换为字符串以json输出

本文关键字:json 输出 字符串 字典 对象 转换 | 更新日期: 2023-09-27 18:04:03

我在webmatrix中使用razor。我有一个字典对象

var occupancyTotalDic = new Dictionary<double, int>();

一旦我用数据填充了字典,我希望输出JSON作为响应来填充一个航路图

Dictionary<String, String> input = new Dictionary<String, String>();
occupancyTotalDic = input.ToDictionary(item => item.Key, item => (String)item.Value);
Response.ContentType = "application/json";
Response.Write(Json.Encode(occupancyTotalDic));

如何将我的字典转换为包含字符串,以便我可以输出为JSON?

将字典对象转换为字符串以json输出

由于Webmatrix无法编译MVC应用程序,我假设您正在使用ASP。净webforms。

尝试此解决方案https://stackoverflow.com/a/5597628/713789

如果你正在使用MVC,那么就使用JsonResult test(){ return Json(myobj);}