zoukankan      html  css  js  c++  java
  • Asp.net mvc 3 JSONResult return array

    publicclassArrayJsonResult:System.Web.Mvc.JsonResult
    {
       
    publicoverridevoidExecuteResult(ControllerContext context)
       
    {
           
    if(context ==null)
           
    {
               
    thrownewArgumentNullException("context");
           
    }
           
    if(JsonRequestBehavior==JsonRequestBehavior.DenyGet&&
               
    String.Equals(context.HttpContext.Request.HttpMethod,"GET",StringComparison.OrdinalIgnoreCase))
           
    {
               
    thrownewInvalidOperationException("JsonRequest_GetNotAllowed");
           
    }

           
    HttpResponseBase response = context.HttpContext.Response;

           
    if(!String.IsNullOrEmpty(ContentType))
           
    {
                response
    .ContentType=ContentType;
           
    }
           
    else
           
    {
                response
    .ContentType="application/json";
           
    }
           
    if(ContentEncoding!=null)
           
    {
                response
    .ContentEncoding=ContentEncoding;
           
    }
           
    if(Data!=null)
           
    {
               
    StringWriter sw =newStringWriter();
                sw
    .Write("[");
               
    try
               
    {
                   
    var collection =DataasIEnumerable<String>;
                   
    int countLessOne = collection.Count()-1;
                   
    for(int i =0; i < countLessOne; i++)
                   
    {
                        sw
    .Write(collection.ElementAt(i));
                        sw
    .Write(",");
                   
    }
                    sw
    .Write(collection.ElementAt(countLessOne));
               
    }
               
    catch(Exception)
               
    {
                   
    //data was not a collection
               
    }

                sw
    .Write("]");
                response
    .Write(sw.ToString());
           
    }
       
    }
  • 相关阅读:
    Redis安装-Redis常用命令-redis.conf配置信息总结
    JVM--心得 OOM时的堆信息获取与分析
    JVM--心得 堆栈区域和GC的设置
    JVM--心得(加载 链接 初始化)
    JVM--心得概念
    我的Python之路:找一个幸运数
    springboot模板
    spring boot入门
    java自定义注解
    git集成idea
  • 原文地址:https://www.cnblogs.com/webglcn/p/2661444.html
Copyright © 2011-2022 走看看