www.91084.com

GVKun编程网logo

Newtonsoft Json.Net序列化JObject即使设置正确也不会忽略null(json序列化 忽略属性)

12

本篇文章给大家谈谈NewtonsoftJson.Net序列化JObject即使设置正确也不会忽略null,以及json序列化忽略属性的知识点,同时本文还将给你拓展.Net使用Newtonsoft.Js

本篇文章给大家谈谈Newtonsoft Json.Net序列化JObject即使设置正确也不会忽略null,以及json序列化 忽略属性的知识点,同时本文还将给你拓展.Net使用Newtonsoft.Json.dll(JSON.NET)对象序列化成json、反序列化json示例教程、asp.net core 3.0 JObject The collection type ''Newtonsoft.Json.Linq.JObject'' is not supported、C# Newtonsoft.Json JObject移除属性,在序列化时忽略、c# – 在过滤掉某些属性时序列化JSON.NET JObject等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

Newtonsoft Json.Net序列化JObject即使设置正确也不会忽略null(json序列化 忽略属性)

Newtonsoft Json.Net序列化JObject即使设置正确也不会忽略null(json序列化 忽略属性)

我正在尝试使用Newtonsoft Json.Net序列化对象。

该对象是一个匿名类型,其中填充了许多异构事物,主要是常规POCO,但也包含JObjects或JArrays。

问题是,将NullValueHandling属性添加到时NullValueHandling.Ignore,每个null属性都将被忽略,但前提是它是“常规”
.Net对象的一部分。JObject或内的每个null属性都会JArray保留。

这是一个最小的示例:

var jobj = JObject.FromObject(new Anything{    x = 1,    y = "bla",    z = null});var poco = new Foo {   foo1 = "bar",   foo2 = null};var serialized = JsonConvert.SerializeObject(new {    source1 = poco,    source2 = jobj}, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore});

是否有一种简单的方法也可以忽略这些空值?我错过了一些设置选项吗?还是我必须手动处理?

答案1

小编典典

一个"null"在值JObject实际上是一个
_非空JValue_与JValue.Type等于JTokenType.Null。当此值实际出现在JSON中时,它表示null的JSON值。我相信它的存在是为了捕获以下两个JSON对象之间的差异:

  "source2": {    "z": null  }  "source2": {  }

在第一种情况下,该属性"z"带有nullJSON值。在第二种情况下,该属性"z"不存在。Linq-to-
JSON表示第一种情况是null类型,JValue而不是JProperty.Value实际 为null

为防止空令牌渗透到您JObject的值中,请在JObject从某些POCO 创建时使用适当的序列化器设置:

var jobj = JObject.FromObject(new{    x = 1,    y = "bla",    z = (int?)null}, new JsonSerializer { NullValueHandling = NullValueHandling.Ignore } );

(请注意,POCO本身本身不一定已经是JObject。无类型方法,JsonConvert.DeserializeObject(jsonString)或者JsonConvert.DeserializeObject<dynamic>(jsonString)默认情况下,JObject当根JSON容器jsonString为JSON对象时,默认情况下将返回a
。)

.Net使用Newtonsoft.Json.dll(JSON.NET)对象序列化成json、反序列化json示例教程

.Net使用Newtonsoft.Json.dll(JSON.NET)对象序列化成json、反序列化json示例教程

JSON作为一种轻量级的数据交换格式,简单灵活,被很多系统用来数据交互,作为一名.NET开发人员,JSON.NET无疑是最好的序列化框架,支持XML和JSON序列化,高性能,免费开源,支持LINQ查询。目前已被微软集成于webapi框架之中,因此,熟练掌握JSON.NET相当重要,这篇文章是零度参考官网整理的示例,通过这些示例,可以全面了解JSON.NET提供的功能。

Newtonsoft.Json的地址:

官网:http://json.codeplex.com/

源码地址:https://github.com/JamesNK/Newtonsoft.Json

Newtonsoft.Json.dll下载:https://github.com/JamesNK/Newtonsoft.Json/releases

1、使用Newtonsoft.Json(JSON.NET)序列化对象,通过Newtonsoft.Json.Formatting将json格式化输出。

            Account account = new Account
            {
                Email = "1930906722@qq.com",Active = true,CreatedDate =DateTime.Now,Roles = new List<string> { "User","Admin" }
            };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(account,Newtonsoft.Json.Formatting.Indented);
            Console.WriteLine(json);
    public class Account
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public bool Active { get; set; }
        public DateTime CreatedDate { get; set; }
        public IList<string> Roles { get; set; }
    }

执行结果:

2、使用Newtonsoft.Json(JSON.NET)序列化List集合:

            List<string> videogames = new List<string> { "HTML5","JavaScript",".net","c#",".net core" };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(videogames);
            Console.WriteLine(json);

执行结果:

3、使用Newtonsoft.Json(JSON.NET)序列化dictionary字典

            System.Collections.Generic.Dictionary<string,string> dic = new System.Collections.Generic.Dictionary<string,string>
            {
                { "Name","张三" },{ "Age","20" },{ "Email","193090622@qq.com" }
            };
            string json1 = Newtonsoft.Json.JsonConvert.SerializeObject(dic,Newtonsoft.Json.Formatting.Indented);
            Console.WriteLine(json1);
            Console.WriteLine("");
            Console.WriteLine("未格式化的json:");
            string json2 = Newtonsoft.Json.JsonConvert.SerializeObject(dic,Newtonsoft.Json.Formatting.None);
            Console.WriteLine(json2);

执行结果:

4、Newtonsoft.Json(JSON.NET)将序列化结果保存到指定的文件:

            User movie = new User { Name = "张三",Age = 1993 };
            using (System.IO.StreamWriter file = System.IO.File.CreateText(@"F:\UserInfo.txt"))
            {
                Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
                serializer.Serialize(file,movie);
            }
    public class User
    {
        public string Name { set; get; }
        public int Age { set; get; }
    }

执行后保存到文件的结果:

5、Newtonsoft.Json(JSON.NET)基于枚举类型的JsonConverters转换器

            List<JosnEnum> list = new List<JosnEnum> { JosnEnum.NotStartus,JosnEnum.Startus };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(list);
            Console.WriteLine(json);
            Console.WriteLine("");

            System.Collections.Generic.Dictionary<string,int> dic = new System.Collections.Generic.Dictionary<string,int>
            { 
            {((JosnEnum)(int)JosnEnum.NotStartus).ToString(),(int)JosnEnum.NotStartus},{((JosnEnum)(int)JosnEnum.Startus).ToString(),(int)JosnEnum.Startus}
            };
            string dicJson = Newtonsoft.Json.JsonConvert.SerializeObject(dic);
            Console.WriteLine(dicJson);

            Console.WriteLine("");
            List<JosnEnum> list2 = new List<JosnEnum>
            {
                JosnEnum.NotStartus,JosnEnum.Startus
            };
            string json3 = Newtonsoft.Json.JsonConvert.SerializeObject(list2,new Newtonsoft.Json.Converters.StringEnumConverter());
            Console.WriteLine(json3);

            Console.WriteLine("");

            List<JosnEnum> result = Newtonsoft.Json.JsonConvert.DeserializeObject<List<JosnEnum>>(json3,new Newtonsoft.Json.Converters.StringEnumConverter());
            Console.WriteLine(string.Join(",",result.Select(c => c.ToString())));
    public enum JosnEnum
    {
        NotStartus = 0,Startus = 1
    }

执行结果:

6、Newtonsoft.Json(JSON.NET)通过JRaw将JS函数序列化到JSON中

            JavaScriptSettings settings = new JavaScriptSettings
            {
                OnLoadFunction = new Newtonsoft.Json.Linq.JRaw("OnLoad"),OnSucceedFunction = new Newtonsoft.Json.Linq.JRaw("function(e) { alert(e); }")
            };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(settings,Newtonsoft.Json.Formatting.Indented);
            Console.WriteLine(json);
    public class JavaScriptSettings
    {
        public Newtonsoft.Json.Linq.JRaw OnLoadFunction { get; set; }
        public Newtonsoft.Json.Linq.JRaw OnSucceedFunction { get; set; }
    }

7、使用Newtonsoft.Json(JSON.NET)将json反序列化对象

  string json = @"{
   'Email': '1930906722@qq.com','Active': true,'CreatedDate': '2016-11-26 20:39','Roles': [
     'User','Admin'
]
 }";
            Account account = Newtonsoft.Json.JsonConvert.DeserializeObject<Account>(json);
            Console.WriteLine(account.Email);
    public class Account
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public bool Active { get; set; }
        public DateTime CreatedDate { get; set; }
        public IList<string> Roles { get; set; }
    }

执行结果:

8、使用Newtonsoft.Json(JSON.NET)反序列化List集合:

            string json = @"['Html5','C#','.Net','.Net Core']";
            List<string> videogames = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(json);
            Console.WriteLine(string.Join(",videogames));

执行结果:

9、使用Newtonsoft.Json(JSON.NET)反序列化dictionary字典

            string json = @"{'Name': '张三','Age': '23'}";
            var htmlAttributes = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string,string>>(json);
            Console.WriteLine(htmlAttributes["Name"]);
            Console.WriteLine(htmlAttributes["Age"]);

执行结果:

10、使用Newtonsoft.Json(JSON.NET)序列化var匿名类型,有时候,我们并不需要先定义一个类,然后new一个对象后再进行序列化,JSON.NET支持匿名类型的序列化和反序列化。

            var test1 = new { Name = "李四",Age = 26 };
            var json = Newtonsoft.Json.JsonConvert.SerializeObject(test1);
            Console.WriteLine(json);

            Console.WriteLine("");
            var test2 = new { Name = "",Age = 0 };
            string json1 = @"{'Name':'张三','Age':'25'}";
            var result = Newtonsoft.Json.JsonConvert.DeserializeAnonymousType(json1,test2);
            Console.WriteLine(result.Name);

执行结果:

11、Newtonsoft.Json(JSON.NET)用新JSON字符串填充指定对象的属性值

            Account account = new Account
            {
                Email = "1930906722@qq.com",CreatedDate = DateTime.Now,"Admin" }
            };
            string json = @"{'Active': false,'Roles': ['Expired']}";
            Newtonsoft.Json.JsonConvert.PopulateObject(json,account);
            Console.WriteLine(account.Active);
            Console.WriteLine(account.Email);
    public class Account
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public bool Active { get; set; }
        public DateTime CreatedDate { get; set; }
        public IList<string> Roles { get; set; }
    }

执行结果:

12、使用Newtonsoft.Json(JSON.NET)反序列化时可指定构造函数:

首先我们定义如下的类型,我们希望JSON.NET反序列化对象时使用第2个构造函数,我们将第一个默认构造函数屏蔽,标记为私有private修饰符。第2个构造函数需要指定一个website对象作为参数,如果提供的参数为null则抛出异常:

public class Website
{
    public string Url { get; set; }
    private Website()
    {
    }
    public Website(Website website)
    {
        if (website == null) throw new ArgumentNullException("website");
        Url = website.Url;
    }
}

现在使用一般的方式反序列化一个JSON字符串。执行出现的结果:

我们发现该序列化方法抛出了异常,并没有按照我们预想的方式进行反序列化,JSON.NET提供如下的方式指定公有构造函数。

            string json = @"{'Url':'http://www.cnblogs.com/linJie1930906722/'}";
            Website website = Newtonsoft.Json.JsonConvert.DeserializeObject<Website>(json,new Newtonsoft.Json.JsonSerializerSettings
            {
                ConstructorHandling = Newtonsoft.Json.ConstructorHandling.AllowNonPublicDefaultConstructor
            });
            Console.WriteLine(website.Url);

执行结果:

另外,JSON.NET提供了指定任何构造函数的JsonConstructorAttribute特性,只需要在构造函数上标记,即可指定构造函数。

    public class Users
    {
        public string UserName { get; private set; }
        public bool Enabled { get; private set; }
        public Users()
        {
        }
        [Newtonsoft.Json.JsonConstructor]
        public Users(string userName,bool enabled)
        {
            UserName = userName;
            Enabled = enabled;
        }
    }
            string json = @"{""UserName"": ""希特勒"",""Enabled"": true}";
            Users user = Newtonsoft.Json.JsonConvert.DeserializeObject<Users>(json);
            Console.WriteLine(user.UserName);

执行结果:

13、当对象的属性为默认值(0或null)时不序列化该属性

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public Person Partner { get; set; }
        public decimal? Salary { get; set; }
    }
            Person person1 = new Person();
            string json1 = Newtonsoft.Json.JsonConvert.SerializeObject(person1,Newtonsoft.Json.Formatting.Indented,new Newtonsoft.Json.JsonSerializerSettings
            {
                DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore
            });
            Console.WriteLine(json1);

            Console.WriteLine("");
            Person person2 = new Person(){Name = "奥巴马"};
            string json2 = Newtonsoft.Json.JsonConvert.SerializeObject(person2,new Newtonsoft.Json.JsonSerializerSettings
            {
                DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore
            });
            Console.WriteLine(json2);

执行结果:

14、Newtonsoft.Json(JSON.NET)中忽略null值得处理器

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public Person Partner { get; set; }
        public decimal? Salary { get; set; }
    }
            Person person = new Person { Name = "张三",Age = 46 };
            string jsonIncludeNullValues = Newtonsoft.Json.JsonConvert.SerializeObject(person,Newtonsoft.Json.Formatting.Indented);
            Console.WriteLine(jsonIncludeNullValues);
            Console.WriteLine("");
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(person,new Newtonsoft.Json.JsonSerializerSettings
            {
                NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
            });
            Console.WriteLine(json);

执行结果:

15、JSON.NET中循环引用的处理方法

            Employee employee1 = new Employee { Name = "张三" };
            Employee employee2 = new Employee { Name = "李四" };
            employee1.Manager = employee2;
            employee2.Manager = employee2;
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(employee1,new Newtonsoft.Json.JsonSerializerSettings
            {
                ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
            });
            Console.WriteLine(json);
    public class Employee
    {
        public string Name { get; set; }
        public Employee Manager { get; set; }
    }

执行结果:

16、通过ContractResolver指定属性名首字母小写,通常,在.NET中属性采用PascalCase规则(首字母大写),在JavaScript中属性名使用CamelCase规则(首字母小写),我们希望序列化后的JSON字符串符合CamelCase规则,JSON.NET提供的ContractResolver可以设置属性名小写序列化

    public class User
    {
        public string Name { set; get; }
        public int Age { set; get; }
    }
            User person = new User { Name = "张三",Age =52 };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(person,new Newtonsoft.Json.JsonSerializerSettings
            {
                ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
            });
            Console.WriteLine(json);

执行结果:

17、JSON.NET中通过特性序列化枚举类型

    public enum ProductStatus
    { 
        NotConfirmed,Active,Deleted
    }

    public class Product
    {
        public string Name { get; set; }

        [Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
        public ProductStatus Status { get; set; }
    }
            Product user = new Product { Name = @"羽绒服",Status = ProductStatus.Deleted };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(user,Newtonsoft.Json.Formatting.Indented);
            Console.WriteLine(json);

执行结果:

18、指定需要序列化的属性

    [Newtonsoft.Json.JsonObject(Newtonsoft.Json.MemberSerialization.OptIn)]
    public class Categroy
    {
        //Id不需要序列化
        public Guid Id { get; set; }

        [Newtonsoft.Json.JsonProperty]
        public string Name { get; set; }

        [Newtonsoft.Json.JsonProperty]
        public int Size { get; set; }
    }
            Categroy categroy = new Categroy
            {
                Id = Guid.NewGuid(),Name = "内衣",Size = 52
            };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(categroy,Newtonsoft.Json.Formatting.Indented);
            Console.WriteLine(json);

执行结果:

19、序列化对象时指定属性名

    public class Videogame
    {
        [Newtonsoft.Json.JsonProperty("name")]
        public string Name { get; set; }

        [Newtonsoft.Json.JsonProperty("release_date")]
        public DateTime ReleaseDate { get; set; }
    }
            Videogame starcraft = new Videogame
            {
                Name = "英雄联盟",ReleaseDate = DateTime.Now
            };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(starcraft,Newtonsoft.Json.Formatting.Indented);
            Console.WriteLine(json);

执行结果:

20、序列化时指定属性在JSON中的顺序

    public class Personl
    {
        [Newtonsoft.Json.JsonProperty(Order = 2)]
        public string FirstName { get; set; }

        [Newtonsoft.Json.JsonProperty(Order = 1)]
        public string LastName { get; set; }
    }
            Personl person = new Personl { FirstName = "张三",LastName = "李四" };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(person,Newtonsoft.Json.Formatting.Indented);
            Console.WriteLine(json);

执行结果:

21、反序列化指定属性是否必须有值必须不为null,在反序列化一个JSON时,可通过JsonProperty特性的required指定反序列化行为,当反序列化行为与指定的行为不匹配时,JSON.NET将抛出异常,required是枚举,required.Always表示属性必须有值切不能为null,required.AllowNull表示属性必须有值,但允许为null值。

    public class Order
    {
        [Newtonsoft.Json.JsonProperty(required = Newtonsoft.Json.required.Always)]
        public string Name { get; set; }

        [Newtonsoft.Json.JsonProperty(required = Newtonsoft.Json.required.AllowNull)]
        public DateTime? ReleaseDate { get; set; }
    }
            string json = @"{
    'Name': '促销订单','ReleaseDate': null
  }";
            Order order = Newtonsoft.Json.JsonConvert.DeserializeObject<Order>(json);
            Console.WriteLine(order.Name);
            Console.WriteLine(order.ReleaseDate);

执行结果:

22、通过特性指定null值忽略序列化

    public class Vessel
    {
        public string Name { get; set; }

        [Newtonsoft.Json.JsonProperty(NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public DateTime? launchdate { get; set; }
    }
            Vessel vessel = new Vessel { Name = "张三" };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(vessel,Newtonsoft.Json.Formatting.Indented);
            Console.WriteLine(json);

执行结果:

23、忽略不需要序列化的属性,并不是对象所有属性都要参与序列化,我们可以使用JsonIgnore特性排除不需要序列化的属性,下面示例中的PasswordHash将被忽略。

    public class Accounts
    {
        public string FullName { get; set; }
        public string EmailAddress { get; set; }
        [Newtonsoft.Json.JsonIgnore]
        public string PasswordHash { get; set; }
    }
            Accounts account = new Accounts
            {
                FullName = "admin",EmailAddress = "1930906722@qq.com",PasswordHash = "dfsfgerhtyhsasdhjyujtgwe454811sfsg8d"
            };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(account);
            Console.WriteLine(json);

执行结果:

24、序列化或反序列化时指定日期时间格式,JSON.NET中提供一个名为JsonSerializerSettings的设置对象,可通过此对象设置很多序列化和反序列化的行为,如果要设置JSON.NET序列化输出的日期时间格式,只需要指定格式化字符串即可。通过JsonSerializerSettings的DateFormatString属性指定日期时间格式:

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime CreateDate { get; set; }
    }
            Customer custom = new Customer { FirstName = "张三",LastName = "李四",CreateDate = DateTime.Now };
            Newtonsoft.Json.JsonSerializerSettings settings = new Newtonsoft.Json.JsonSerializerSettings
            {
                DateFormatString = "yyyy-MM-dd HH:mm:ss",Formatting = Newtonsoft.Json.Formatting.Indented
            };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(custom,settings);
            Console.WriteLine(json);

执行结果:

asp.net core 3.0 JObject The collection type ''Newtonsoft.Json.Linq.JObject'' is not supported

asp.net core 3.0 JObject The collection type ''Newtonsoft.Json.Linq.JObject'' is not supported

在asp.net core 3.0 中,如果直接在Controller中返回 Jobject 类型,会抛出如下错误:


The collection type ''Newtonsoft.Json.Linq.JObject'' is not supported.
System.NotSupportedException: The collection type ''Newtonsoft.Json.Linq.JObject'' is not supported.
at System.Text.Json.JsonPropertyInfoNotNullable`4.GetDictionaryKeyAndValueFromGenericDictionary(WriteStackFrame& writeStackFrame, String& key, Object& value)
at System.Text.Json.JsonPropertyInfo.GetDictionaryKeyAndValue(WriteStackFrame& writeStackFrame, String& key, Object& value)
at System.Text.Json.JsonSerializer.HandleDictionary(JsonClassInfo elementClassInfo, JsonSerializerOptions options, Utf8JsonWriter writer, WriteStack& state)
at System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer, Int32 originalWriterDepth, Int32 flushThreshold, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteAsyncCore(Stream utf8Json, Object value, Type inputType, JsonSerializerOptions options, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)

该问题的出现估计与.net 3.0 新引入的System.Text.Json类库有关.

折衷的解决办法是:

使用Content方法将 JObject 类型的返回值转为 ContentResult 类型.

伪代码如下:


[HttpPost]

public ContentResult Method1([FromBody]Param param1)
{

  JObject result=xxx;

  return Content(result.ToString());

}

如果有时间,建议跟一下System.Text.Json类库的源码来彻底解决该问题.

C# Newtonsoft.Json JObject移除属性,在序列化时忽略

C# Newtonsoft.Json JObject移除属性,在序列化时忽略

原文 C# Newtonsoft.Json JObject移除属性,在序列化时忽略

一、针对 单个 对象移除属性,序列化时忽略处理

JObject实例的 Remove() 方法,可以在 指定序列化时移除属性和值

示例如下 :

[csharp]  view plain  copy
  1. //json 序列化  
  2. JObject obj1 = JObject.FromObject(new  
  3. {  
  4.     id = 1,  
  5.     name = "张三",  
  6.     age = 20  
  7. });  
  8. Console.WriteLine(obj1.ToString());  
  9. //输出:  
  10. //{  
  11. //  "id": 1,  
  12. //  "name": "张三",  
  13. //  "age": 20  
  14. //}  
  15.   
  16.   
  17. //指定忽略对象的属性  
  18. //注:指定的key区分大小写  
  19. obj1.Remove("id");  
  20. Console.WriteLine(obj1.ToString());  
  21. //输出结果:  
  22. //{  
  23. //    "name": "张三",  
  24. //    "age": 20  
  25. //}  

注 :1.属性移除仅对当前 对象的属性起作用

2.指定移除 的 key区分 大小写,移除的属性需要和属性 大小写一直

如想 删除二级数组对象的属性,可以参考 代码:

[csharp]  view plain  copy
  1. JArray jarr = obj1.SelectToken("images") as JArray;  
  2. if (jarr != null)  
  3.     foreach (JObject item in jarr)  
  4.     {  
  5.         item.Remove("id");  
  6.     }  

 

 

二、使用 [Newtonsoft.Json.JsonIgnore()]   ,属性标注用于忽略处理

[csharp]  view plain  copy
  1. [Newtonsoft.Json.JsonIgnore()]  
  2. public int ID { get; set; }  

更多:

C# Newtonsoft.Json JObject合并对象整理

C# dynamic类型序列化和反序列化之Newtonsoft.Json

 

C# Newtonsoft.Json JsonSerializerSettings配置序列化操作

c# – 在过滤掉某些属性时序列化JSON.NET JObject

c# – 在过滤掉某些属性时序列化JSON.NET JObject

我的代码中有一个大的任意 JSON结构作为JObject引用.

我想序列化这个结构,除非我遇到一个包含一个名为type的属性的JObject,其值为“encrypted”,然后我想在写入对象之前删除相邻的data属性.

换句话说,如果我遇到这个:

{
  type: "encrypted",name: "some-name",data: "<base64-string>"
}

它将被序列化为:

{
  type: "encrypted",name: "some-name"
}

我不能改变结构,在变异之前克隆它会效率太低,所以我尝试使用JsonConverter如下:

public class RemoveEncryptedDataSerializer : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(JObject);
    }

    public override object ReadJson(JsonReader reader,Type objectType,object existingValue,JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override void WriteJson(JsonWriter writer,object value,JsonSerializer serializer)
    {
        var o = (JObject)value;
        if (o.Value<string>("type") != "encrypted")
        {
            o.Writeto(writer);
            return;
        }

        var copy = o.DeepClone();
        copy["data"]?.Parent.Remove();
        copy.Writeto(writer);
    }
}

但是,CanConvert函数似乎只是使用不是从JToken派生的类型调用,所以我的WriteJson函数永远不会被调用.

还有另一种方法来实现这一目标吗?

编辑:以下是一些可用于测试的代码:

[TestMethod]
public void ItShouldExcludeEncryptedData()
{
    var input = JObject.Parse(@"
    {
        a: {
            type: 'encrypted',name: 'some-name',data: 'some-data'
        }
    }");

    var expected = JObject.Parse(@"
    {
        a: {
            type: 'encrypted',}
    }");

    var output = input.ToString(Formatting.Indented,new RemoveEncryptedDataSerializer());

    Assert.AreEqual(
        expected.ToString(Formatting.Indented),output);
}

解决方法

需要构建转换器来处理JToken,它必须递归工作以确保编辑所有加密数据.

我能够使用以下转换器:

public class RemoveEncryptedDataConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return typeof(JToken).IsAssignableFrom(objectType);
    }

    public override object ReadJson(JsonReader reader,JsonSerializer serializer)
    {
        JToken token = (JToken)value;
        if (token.Type == JTokenType.Object)
        {
            bool omitDataProperty = token.Value<string>("type") == "encrypted";

            writer.WriteStartObject();
            foreach (var prop in token.Children<JProperty>())
            {
                if (omitDataProperty && prop.Name == "data")
                    continue;

                writer.WritePropertyName(prop.Name);
                serializer.Serialize(writer,prop.Value);  // recurse
            }
            writer.WriteEndobject();
        }
        else if (token.Type == JTokenType.Array)
        {
            writer.WriteStartArray();
            foreach (var item in token.Children())
            {
                serializer.Serialize(writer,item);  // recurse
            }
            writer.WriteEndarray();
        }
        else // JValue
        {
            token.Writeto(writer);
        }
    }
}

工作演示:https://dotnetfiddle.net/0K61Bz

如果要直接通过流使用JsonWriter,可以将转换器中的逻辑重构为递归扩展方法并使用它.如果您不使用序列化程序,则不需要转换器.

public static class JsonExtensions
{
    public static void RedactedWriteto(this JToken token,JsonWriter writer)
    {
        if (token.Type == JTokenType.Object)
        {
            bool omitDataProperty = token.Value<string>("type") == "encrypted";

            writer.WriteStartObject();
            foreach (var prop in token.Children<JProperty>())
            {
                if (omitDataProperty && prop.Name == "data")
                    continue;

                writer.WritePropertyName(prop.Name);
                prop.Value.RedactedWriteto(writer);  // recurse
            }
            writer.WriteEndobject();
        }
        else if (token.Type == JTokenType.Array)
        {
            writer.WriteStartArray();
            foreach (var item in token.Children())
            {
                item.RedactedWriteto(writer);  // recurse
            }
            writer.WriteEndarray();
        }
        else // JValue
        {
            token.Writeto(writer);
        }
    }
}

然后你就可以像这样使用它,其中stream是你的输出流,输入是你的JObject:

using (StreamWriter sw = new StreamWriter(stream))  // or StringWriter if you prefer
using (JsonWriter writer = new JsonTextWriter(sw))
{
    writer.Formatting = Formatting.Indented;
    input.RedactedWriteto(writer);
}

小提琴:https://dotnetfiddle.net/l949HU

关于Newtonsoft Json.Net序列化JObject即使设置正确也不会忽略nulljson序列化 忽略属性的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于.Net使用Newtonsoft.Json.dll(JSON.NET)对象序列化成json、反序列化json示例教程、asp.net core 3.0 JObject The collection type ''Newtonsoft.Json.Linq.JObject'' is not supported、C# Newtonsoft.Json JObject移除属性,在序列化时忽略、c# – 在过滤掉某些属性时序列化JSON.NET JObject的相关信息,请在本站寻找。

本文标签: