使用Newtonsoft.Json.dll序列化为json时过滤掉NULL值的属性
using Newtonsoft.Json;
namespace WebApi.Controllers
{
public class Rootobject
{
public string PhoneNumber { get; set; }
public string Name { get; set; }
public string MF { get; set; }
public string BirthDate { get; set; }
public object MaritalStatus { get; set; }
public object Country { get; set; }
}
[Route("api/[controller]/[action]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpGet]
public IEnumerable<string> Get()
{
var jsonSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
var data = new Rootobject() {PhoneNumber="13800138000", Name="测试" };
var json = JsonConvert.SerializeObject(data, Formatting.Indented,jsonSetting);
return new string[] { json };
}
}
}
//结果: { "PhoneNumber": "13800138000", "Name": "测试" }
使用Newtonsoft.Json.dll序列化为json时过滤掉NULL值的属性
本文2024-09-16 17:45:35发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-17758.html