Tag: json.net

使用NewtonSoft.JSON序列化接口/抽象对象

反序列化接口和抽象属性的一种方法是通过在序列化和反序列化期间将TypeNameHandling设置为Auto来实现。 但是,当我在直接序列化和反序列化接口对象时尝试相同的操作时,它不起作用 – interface ISample { string Key { get; set; } } class A : ISample { public string Key { get; set; } public A(string key) { this.Key = key; } } class B : ISample { public string Key { get; set; } public B(string key) { this.Key = key; } } […]

JSON.NET序列化空JSON

我使用MetadataType为以下类型定义Json.NET属性,然后在其ToString()方法中使用Json.NET对其进行序列化: namespace ConsoleApp1 { public interface ICell { int Id { get; } } public interface IEukaryote { System.Collections.Generic.IEnumerable Cells { get; } string GenericName { get; } } public sealed partial class PlantCell : ICell { public int Id => 12324; } public sealed partial class Plant : IEukaryote { private readonly System.Collections.Generic.IDictionary _valuesDict; […]

如何使用Json.Net将JSON数组反序列化为对象?

我有一个有效的JSON对象,里面有一个JSON数组。 JSON数组没有花括号,并且在其中包含逗号分隔的混合类型列表。 它看起来像这样: { “ID”: 17, “Days”: 979, “Start_Date”: “10/13/2012”, “End_Date”: “11/12/2012”, “State”: “”, “Page”: 1, “Test”: “Valid”, “ROWS”: [ [441210, “A”, “12/31/2009”, “OK”, “Done”, “KELLEY and Co'”, “12/31/2009”, “06/29/2010”, “TEXAS”, “Lawyers”, 6, “”, “”], [441151, “B”, “12/31/2009”, “No”, “In-process”, “Sage & Co”, “12/31/2009”, “06/29/2010”, “CALIFORNIA”, “Realtor”, 6, “”, “”] ] } 我已经创建了一个反映JSON结构的类,它有一个复杂数组的List: class myModel […]

SignalR Typenamehandling

我试图让SignalR与自定义JsonSerializerSettings一起使用它的有效负载,特别是我试图设置TypeNameHandling = TypeNameHandling.Auto 。 问题似乎是,SignalR使用hubConnection.JsonSerializer和hubConnection.JsonSerializer GlobalHost.DependencyResolver.Resolve()作为其内部数据结构,然后导致各种破坏(当我设置TypeNameHandling.All时内部服务器崩溃)作为最粗鲁的例子,但是使用TypeNameHandling.Auto我也会遇到问题,特别是当涉及IProgress回调时)。 是否有任何解决方法或我只是做错了? 用于演示的示例代码: 服务器: class Program { static void Main(string[] args) { using (WebApp.Start(“http://localhost:8080”)) { Console.ReadLine(); } } } public class Startup { public void Configuration(IAppBuilder app) { var hubConfig = new HubConfiguration() { EnableDetailedErrors = true }; GlobalHost.DependencyResolver.Register(typeof(JsonSerializer), ConverterSettings.GetSerializer); app.MapSignalR(hubConfig); } } public interface IFoo { string Val { […]

C#中的JSON反序列化如何工作

我试图了解JsonConvert.DeserializeObject(someJsonString)如何能够使用构造函数设置值。 using Newtonsoft.json public class X { [JsonProperty(“some_Property”)] public string SomeProperty {get;} [JsonProperty(“some_Property_2”)] public string SomeProperty2 {get;} public X(string someProperty, string someProperty2) { SomeProperty = someProperty; SomeProperty2 = someProperty2; } public static X parseObject(string parseThisJson) { JsonConvert.DeserializeObject(someJsonString); } } 在上面的代码中,我想了解JsonConvert.DeserializeObject如何能够正确地反序列化它。 json序列化是否使用此public X(string someProperty, string someProperty2)构造函数? 如果是这样,如何调用和使用此构造函数? 会发生什么是parseThisJson除了some_Property和some_Property_2之外还有更多的键值对?

自定义反序列化器仅适用于json.NET的某些字段

我正在尝试反序列化一些JSON: { “a”:1, “b”:25, “c”:”1-7″, “obj1”:{ “a1”:10, “b1”:45, “c1”:60 }, “obj2”:[ { “a2”:100, “b2”:15, “c2”:50 }, { “e2″:”1,2,5-7”, “f2″:”1,3-5”, “a2”:25 } ] } 我想找到一种方法来定义仅针对某些字段的自定义反序列化。 在下面的代码中,我将需要注意的字段(自定义处理)和可以以某种方式自动完成的字段分开。 是否可以自动反序列化“正常”字段? (不需要任何特定的自定义处理) [JsonConverter(typeof(ConfigurationSerializer))] public class Configuration { public int a { get; set; } public int b { get; set; } public Obj1 obj1 { get; set; } public int[] […]

使用Json.NET使用新的部分JSON数据修改现有对象

考虑下面的示例程序 var calendar = new Calendar { Id = 42, CoffeeProvider = “Espresso2000”, Meetings = new[] { new Meeting { Location = “Room1”, From = DateTimeOffset.Parse(“2014-01-01T00:00:00Z”), To = DateTimeOffset.Parse(“2014-01-01T01:00:00Z”) }, new Meeting { Location = “Room2”, From = DateTimeOffset.Parse(“2014-01-01T02:00:00Z”), To = DateTimeOffset.Parse(“2014-01-01T03:00:00Z”) }, } }; var patch = @”{ ‘coffeeprovider’: null, ‘meetings’: [ { ‘location’: […]

使用Json.NET的Pascal案例动态属性

这就是我所拥有的: using Newtonsoft.Json; var json = “{\”someProperty\”:\”some value\”}”; dynamic deserialized = JsonConvert.DeserializeObject(json); 这很好用: Assert.That(deserialized.someProperty.ToString(), Is.EqualTo(“some value”)); 我希望这可以工作(属性大写的第一个字母)而不改变json : Assert.That(deserialized.SomeProperty.ToString(), Is.EqualTo(“some value”));

使用DataContractJsonSerializer反序列化变量Type JSON数组

我有一个这种forms的JSON字符串: string jsonStr = “[\”A\”, [\”Martini\”, \”alovell\”],[\”Martin\”, \”lovell\”]]” 我试图使用C#.NET反序列化器DataContractJsonSerializer使用以下代码片段反序列化JSON MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonStr)); DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof); X data = (X)serializer.ReadObject(ms); 既然JSON数组是一个变量类型的数组,我不知道X应该是什么类型的对象 如果我的字符串是 jsonStr = “[[\”Martini\”, \”alovell\”],[\”Martin\”, \”lovell\”]]” 我可以用这个: X = List<List> 这对我有用。 我想知道是否有任何方法可以反序列化变量类型的JSON数组?

JSON.NET在忽略null属性的同时序列化JObject

我有一个JObject ,用作调用RESTful Web服务的模板 。 这个JObject是通过解析器创建的,因为它用作模板告诉用户端点模式是什么样的,我不得不想办法保留所有属性,这就是为什么我将它们的值默认为null 。 例如,这就是对象最初的样子: { “Foo”:{ “P1”:null, “P2”:null, “P3”:null, “P4”:{ “P1”:null, “P2”:null, “P3”:null, }, “FooArray”:[ { “F1”:null, “F2”:null, “F3”:null, } ] }, “Bar”:null } 然后,用户可以根据需要填写单个字段,例如Foo.P2和Foo.P4.P1 : { “Foo”:{ “P1”:null, “P2″:”hello world”, “P3”:null, “P4”:{ “P1”:1, “P2”:null, “P3”:null, }, “FooArray”:[ { “F1”:null, “F2”:null, “F3”:null, } ] }, “Bar”:null } 意思是他们只关心这两个领域。 现在我想将此模板( JObject )序列化为JSON字符串,但只想要填充的那些字段显示出来。 所以我尝试了这个: […]