使用 return Json(pageList, JsonRequestBehavior.AllowGet); 返回object
使用 return Content(JsonConvert.SerializeObject(pageList, timeConverter)); //返回json字符串
// GET: Home public ActionResult Index() { return View(); } public ActionResult Json() { Dictionary<string, object> dic = new Dictionary<string, object>(); dic.Add("id", 100); dic.Add("name", "hello"); return Json(dic, JsonRequestBehavior.AllowGet); //返回结果 { "id":100,"name":"hello"} } public ActionResult JsonA() { List<string> mList = new List<string>(); mList.Add("111"); //添加一个元素 mList.Insert(1, "laiyanbin");//在index位置添加一个元素 //插入元素 //mList.RemoveAt(1); //删除下标为index的元素 //mList.Remove("111");//删除一个值 //mList.RemoveRange(3, 2);//超出删除的范围会出错 从3开始 删除2个元素 //注:删除某元素后,其后面的元素下标自动跟进 // mList.Contains("111");//判断是否存在 111 //mList.Clear();//清空集合 mList.Count();//获取元素数目 return Json(mList, JsonRequestBehavior.AllowGet); //返回结果 ["111","laiyanbin"] } public ActionResult JsonB() { //test a = new test() { aa = 1, bb = "11", cc = true, dd = "22" };//对象初始化 //var test=new {Title=“a”,author=”b”};匿名对象 // List<Course> mList = new List<Course>(); Course cou1 = new Course() {Cno=1,Cname="123",Score=100 }; mList.Add(cou1); Course cou2 = new Course() { Cno = 2, Cname = "456", Score = 1000 }; mList.Add(cou2); return Json(mList, JsonRequestBehavior.AllowGet); // 返回结果 [{"Cname":"123","Score":100,"Cno":1}] 添加1个 //[{"Cname":"123","Score":100,"Cno":1},{"Cname":"456","Score":1000,"Cno":2}] 添加两个 } public ActionResult JsonC() { DataTable dt = new DataTable(); dt.Columns.Add(); dt.Columns.Add(); dt.Columns.Add(); dt.Columns.Add(); //Data dt.Rows.Add(1, "Manish", "Hyderabad", "0000000000"); dt.Rows.Add(2, "Venkat", "Hyderabad", "111111111"); dt.Rows.Add(3, "Namit", "Pune", "1222222222"); dt.Rows.Add(4, "Abhinav", "Bhagalpur", "3333333333"); return Json(dt, JsonRequestBehavior.AllowGet); //无法输出数据 //只能 输出集合和字典 } } public class Course { public int Cno { get; set; }//课程号 public string Cname;//课程名 public int Score;//成绩 }