返回适当的json

时间:2015-02-07 17:51:35

标签: javascript c# json asp.net-mvc razor

我有以下代码:

    [HttpGet]
    public JsonResult ReturnJson()
    {
        List<Tuple<string, int>> list = new List<Tuple<string,int>>();
        list.Add(new Tuple<string, int>("Feb 2 to Feb 6", 1));
        list.Add(new Tuple<string, int>("Feb 7 to Feb 15", 10));
        list.Add(new Tuple<string, int>("Feb 16 to Feb 24", 4));

        return Json(list, JsonRequestBehavior.AllowGet);
    }

它返回以下字符串:

[{"Item1":"Feb 2 to Feb 6","Item2":1},{"Item1":"Feb 7 to Feb 15","Item2":10},{"Item1":"Feb 16 to Feb 24","Item2":4}]

在我的视图中我应该以格式(javascript)

获取此信息
var data = [["Jan 26 to Jun 30", 8], ["Feb 2 to Feb 6", 15], ["Feb 9 to Feb 13", 16], ["Feb 16 to Feb 20", 7], ["Feb 23 to Feb 27", 16], ["Mar 2 to Mar 6", 8], ["Mar 9 to Mar 13", 4], ["Mar 16 to Mar 20", 15]];

如何在服务器端或客户端将第一个字符串转换为第二个字符串? 感谢

1 个答案:

答案 0 :(得分:1)

将元组更改为列表:

List<List<object>> list = new List<List<object>>();
list.Add(new List<object> {"Feb 2 to Feb 6", 1});
list.Add(new List<object> {"Feb 7 to Feb 15", 10});
list.Add(new List<object> {"Feb 16 to Feb 24", 4});

return Json(list, JsonRequestBehavior.AllowGet);