zoukankan      html  css  js  c++  java
  • .net mvc web api 返回 json 内容时过滤值为null的属性

    1.响应内容(过滤前)

    {"msg":"初始化成功!","code":"","success":true,data:null}

     

    2.响应内容(过滤后)

    {"msg":"初始化成功!","code":"","success":true}

      1. using System.Net.Http.Formatting;  
      2. using System.Web.Http;  
      3.   
      4. namespace xxxxxxx  
      5. {  
      6.     public static class WebApiConfig  
      7.     {  
      8.         public static void Register(HttpConfiguration config)  
      9.         {  
      10.         ......  
      11.   
      12.             //默认返回 json  
      13.             GlobalConfiguration.Configuration.Formatters  
      14.                 .JsonFormatter.MediaTypeMappings.Add(  
      15.                 new QueryStringMapping("datatype", "json", "application/json"));  
      16.             //返回格式选择  
      17.             GlobalConfiguration.Configuration.Formatters  
      18.                 .XmlFormatter.MediaTypeMappings.Add(  
      19.                 new QueryStringMapping("datatype", "xml", "application/xml"));  
      20.             //json 序列化设置  
      21.             GlobalConfiguration.Configuration.Formatters  
      22.                 .JsonFormatter.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings()  
      23.                 {  
      24.                     NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore //设置忽略值为 null 的属性  
      25.                 };  
      26.         }  
      27.     }  
      28. }  
  • 相关阅读:
    NumPy使用图解教程
    Pytorch 网络结构可视化
    OpenCV基于字节指针进行高效像素遍历
    scrapy工具创建爬虫工程
    网络爬虫基本概念与Scrapy工具包使用
    CentOS 7命令行安装GNOME桌面
    sicily 1154. Easy sort (tree sort& merge sort)
    全连通图求最小生成树边权之积(邻接矩阵/prim/kruskal)
    sicily 1024. Magic Island
    写在Web考试后的一点小总结
  • 原文地址:https://www.cnblogs.com/chaoshenzhu/p/7464230.html
Copyright © 2011-2022 走看看