当前位置:   article > 正文

asp.net mvc返回Json数据_return json();

return json();

asp.net mvc返回Json数据

主要介绍了mvc使用JsonResult返回Json数据。

00新建控制器DefaultController

controller 中定义以下方法:

01返回一个自定义的object数组

//01返回一个自定义的object数组
public ActionResult Index()
{
    var res = new JsonResult();
    var name = "小华";

    var age = "27";

    var name1 = "小明";

    var age1 = "26";

    res.Data = new object[] { new { name, age }, new { name1, age1 } };//返回一个自定义的object数组
    res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许使用GET方式获取,否则用GET获取是会报错。

    return res;


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

预览
/default/index
在这里插入图片描述

02返回单个对象;

//02返回单个对象;
   public ActionResult Index2()
    {
        var res = new JsonResult();
        var person = new { Name = "小明", Age = 27, Sex = "男" };

        res.Data = person;//返回单个对象;
        res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许使用GET方式获取,否则用GET获取是会报错。

        return res;


    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

预览
/default/index2
在这里插入图片描述

03返回一个字符串,意义不大;

   //返回一个字符串,意义不大;
    public ActionResult Index3()
    {
        var res = new JsonResult();
        res.Data = "这是个字符串";//返回一个字符串,意义不大;


        res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许使用GET方式获取,否则用GET获取是会报错。

        return res;


    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

预览
/default/index3
在这里插入图片描述

完整代码
DefaultController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC之接口返回json数据.Controllers
{
    public class DefaultController : Controller
    {
        // GET: Default
        //01返回一个自定义的object数组
        public ActionResult Index()
        {
            var res = new JsonResult();
            var name = "小华";

            var age = "27";

            var name1 = "小明";

            var age1 = "26";

            res.Data = new object[] { new { name, age }, new { name1, age1 } };//返回一个自定义的object数组
            res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许使用GET方式获取,否则用GET获取是会报错。

            return res;


        }


        //02返回单个对象;
        public ActionResult Index2()
        {
            var res = new JsonResult();
            var person = new { Name = "小明", Age = 27, Sex = "男" };

            res.Data = person;//返回单个对象;
            res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许使用GET方式获取,否则用GET获取是会报错。

            return res;


        }
       //03返回一个字符串,意义不大;
        public ActionResult Index3()
        {
            var res = new JsonResult();
            res.Data = "这是个字符串";//返回一个字符串,意义不大;


            res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许使用GET方式获取,否则用GET获取是会报错。

            return res;


        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/550315
推荐阅读
相关标签
  

闽ICP备14008679号