zoukankan      html  css  js  c++  java
  • MVC中返回Json的几种声明方式

    第一种,单行数据

     
    var result = new { name = "linfei", age = "26", address = "wuhan" };
    //MVC中返回 return Json(result);
    //asp.net中返回 System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer(); return js.Serialize(result);
     

    显示结果:

    {
        "name": "linfei",
        "age": "26",
        "address": "wuhan"
    }

    第二种,多行数据

     
    var result = new object[] { new { name = "linfei", age = "22", address = "wuhan" }, new { name = "linfei", arg = "26", address = "sh" } };
    //MVC中返回 return Json(result);
    //asp.net中返回 System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer(); return js.Serialize(result);
     

    显示结果:

     
    [
        {
            "name": "linfei",
            "age": "22",
            "address": "wuhan"
        },
        {
            "name": "linfei",
            "arg": "26",
            "address": "sh"
        }
    ]
     

    第三种,多行多对象数据

     
    var data1 = new object[] { new { name = "linfei", age = "22", address = "wuhan" }, new { name = "linfei", arg = "26", address = "sh" } };
    var data2 = new object[] { new { test="123",test1="qq"}, new { test = "456", test1 = "ww" } };
    var result = new object[] { new { rows = data1 }, new { header = data2 } };
    //MVC中返回
    return Json(result);
    //asp.net中返回
    System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
    return js.Serialize(result);
     

     显示结果:

     
    [
        {
            "rows": [
                {
                    "name": "linfei",
                    "age": "22",
                    "address": "wuhan"
                },
                {
                    "name": "linfei",
                    "arg": "26",
                    "address": "sh"
                }
            ]
        },
        {
            "header": [
                {
                    "test": "123",
                    "test1": "qq"
                },
                {
                    "test": "456",
                    "test1": "ww"
                }
            ]
        }
    ]
     

     还有个asp.net中的写法,网上找的

    var data1 = new object[] { new { name = "linfei", age = "22", address = "wuhan" }, new { name = "linfei", arg = "26", address = "sh" } };
    var data2 = new object[] { new { test = "123", test1 = "qq" }, new { test = "456", test1 = "ww" } };
    Newtonsoft.Json.JsonSerializerSettings settings = new Newtonsoft.Json.JsonSerializerSettings();
    settings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
    string result = Newtonsoft.Json.JsonConvert.SerializeObject(new { rows = data1, header = data2 }, Newtonsoft.Json.Formatting.Indented, settings);
  • 相关阅读:
    django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
    Error fetching command 'collectstatic': You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path. Command 'collectstatic' skipped
    windows 虚拟环境下 安装 mysql 引擎一系列错误处理
    项目概念流程
    pip 使用
    HTTPserver v3.0 版本项目
    GitHub 使用
    git 操作命令详解
    git 忽略部分文件类型的同步
    Python 正则处理_re模块
  • 原文地址:https://www.cnblogs.com/haiyabtx/p/3086471.html
Copyright © 2011-2022 走看看