2015-02-07 50 views
0

我有以下代碼:返回正確的JSON

[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

元組是不是數組。它看起來像你想要一個列表(或數組)的列表,而不是元組列表。 – 2015-02-07 17:53:33

回答

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);