mvc使用JsonResult返回Json数据

controller 中定义以下方法:

Html代码
  1. public JsonResult UpdateSingle(int id, string actionName, string actionValue)
  2. {
  3. var res = new JsonResult();
  4. //var value = "actionValue";
  5. //db.ContextOptions.ProxyCreationEnabled = false;
  6. var list = (from a in db.Articles
  7. select new
  8. {
  9. name = a.ArtTitle,
  10. yy = a.ArtPublishTime
  11. }).Take(5);
  12. //记得这里要select new 否则会报错:序列化类型 System.Data.Entity.DynamicProxies XXXXX 的对象时检测到循环引用。
  13. //不select new 也行的加上这句 //db.ContextOptions.ProxyCreationEnabled = false;
  14. res.Data = list;//返回列表
  15.  
  16. var name = "小华";
  17. var age = "12";
  18. var name1 = "小华";
  19. var age1 = "12";
  20. res.Data = new object[] { new { name, age }, new { name1, age1 } };//返回一个自定义的object数组
  21.  
  22. var person = new { Name = "小明", Age = 22, Sex = "男" };
  23. res.Data = person;//返回单个对象;
  24.  
  25. res.Data = "这是个字符串";//返回一个字符串,意义不大;
  26.  
  27. res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许使用GET方式获取,否则用GET获取是会报错。
  28. return res;
  29. }

页面调用:

Html代码
  1. <a href="javascript:void(0);" onclick="javascript:upclick(this);">Click Me</a>
  2. <script type="text/javascript">
  3. function upclick(o) {
  4. var obj = $(o);
  5. alert(obj);
  6. $.ajax({
  7. url: "/Articles/UpdateSingle?ran=" + Math.random(),
  8. type: "GET",
  9. dataType: "json",
  10. data: { id: obj.attr("id"), actionName: obj.attr("actionName"), actionValue: obj.attr("actionValue") },
  11. success: function (data) {
  12. // if (data.result == "True") {
  13. // alert("修改成功!");
  14. // }
  15. // if (obj.attr("actionName") == "ArtVerify") {
  16.  
  17. // }
  18. $(o).html(data[0].name);
  19. obj.attr("actionValue", data[0].result);
  20. }
  21. })
  22. }
  23. </script>

以上是在mvc中使用,在webform中怎么使用呢?
在webform中要引用Newtonsoft.Json.dll;
当然你也可以自己拼接字符串。

Csharp代码
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. var customer = new customer { name = "李华", sex = "男" };
  4. var customer1 = new customer { name = "小芳", sex = "女" };
  5. var li = new List<customer>();
  6. li.Add(customer);
  7. li.Add(customer1);
  8. var list = Newtonsoft.Json.JavaScriptConvert.SerializeObject(li);
  9. var tt = "[{\"name\":\"李华\",\"sex\":\"男\"},{\"name\":\"小芳\",\"sex\":\"女\"}]";
  10. //new Newtonsoft.Json.JsonSerializer()..(customer);
  11. Response.Write(tt);
  12. Response.End();
  13. }
  14.  
  15. public class customer
  16. {
  17. public string name { get; set; }
  18. public string sex { get; set; }
  19. }

页面方法:

Html代码
  1. <p>
  2. <a href="javascript:void(0)" onclick="javascript:getJsonData();">GetJsonData</a>
  3. </p>
  4. <div id="dataDiv">
  5. ggg
  6. </div>
  7. <script type="text/javascript">
  8. function getJsonData() {
  9. var str = "";
  10. $.getJSON("/Json.aspx", function (data) {
  11. var tt = "";
  12. $.each(data, function (k, v) {
  13. $.each(v, function (kk, vv) {
  14. tt += kk + ":" + vv + "<br/>";
  15. });
  16. });
  17. $("#dataDiv").html(tt);
  18. });
  19. }
  20. </script>

显示结果: