JS 代码:
<script src="~/Scripts/jquery-1.8.2.min.js"></script> <script> //ajax提交数据 function SubmitStuAnswersAjax(starlist) { $.post("index4", { stu: starlist }, function (data) { alert(data); }); } //按钮点击方法 function SubmitStuAnswersBtn() { var obg = new Object; obg.name = "黎明"; obg.id = 1; obg.del = false; var obg2 = new Object; obg2.name = "黎明2"; obg2.id = 1; obg2.del = true; var obg3 = new Object; obg3.name = "黎明3"; obg3.id = 1; obg3.del = true; var star = new Array(); star.push(obg) star.push(obg2) star.push(obg3) //将数组序列化json字符串 var starlist= JSON.stringify(star) SubmitStuAnswersAjax(starlist); //console.log(stuAnswerArr) } </script> <input type="button" value="sofdjosdif" onclick="SubmitStuAnswersBtn() " />
后端代码 :
using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Script.Serialization; public ActionResult index4(string stu) { var name = ""; /// 解析JSON数组生成对象实体集合 JsonSerializer serializer = new JsonSerializer(); StringReader sr = new StringReader(stu); object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List<stuEntity>)); List<stuEntity> list = o as List<stuEntity>; foreach (var item in list) { name += item.name + "_"; } return Content(name); } public class stuEntity { public int id { set; get; } public string name { set; get; } public bool del { set; get; } }