Tag: json

当使用.NET Newtonsoft.Json组件反序列化一些有效的json时,为什么我的POCO中的所有集合都为null

问题 当我尝试使用Newtonsoft的Json.NET nuget库反序列化一些Json时,当我不提供JsonSerializerSettings时,我的collection-properties为null – 为什么? 细节 我有一些有效的json数据并将其反序列化到我的自定义类/ POCO中。 现在,所有简单属性(如string s, int等)都可以正确设置。 我的简单子类也可以正确设置(例如, User属性或BuildingDetails属性等)。 但是,我的所有collections都是null 。 从来没有调用过setter(我在那里设置了一个断点,它们没有被设置,但是其他的被调用/设置)。 例如。 属性。 List Images { get { … } set { … } } 例如。 var foo = new Foo(); var json = JsonConvert.Serialize(foo); var anotherFoo = JsonConvert.Deserialize(json); // the collection properties on anotherFoo are null 现在 – 这是疯狂的事情:当我使用JsonSerializerSettings它现在可以工作: […]

JSON序列化类inheritance列表的属性

我有一个模型如下: public class TestResultModel { public bool Successful { get; set; } public string ErrorMessage { get; set; } } public class TestResultListModel : List { public int TotalTestCases { get { return base.Count; } } public int TotalSuccessful { get { return base.FindAll(t => t.Successful).Count; } } } 我从ApiController返回这个TestResultListModel : var testResultListModel = new […]

使用反序列化IEnumerable 不起作用

相当新的Json.net并尝试以下简单示例序列化然后解除对象获取以下错误: using Microsoft.VisualStudio.TestTools.UnitTesting; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Collections; namespace Timehunter.Base.ServicesTests { /// /// Summary description for JsonError /// [TestClass] public class JsonError { [TestMethod] public void TestMethod1() { JsonSerializerSettings serializerSettings = new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.IsoDateFormat, DateParseHandling = Newtonsoft.Json.DateParseHandling.DateTimeOffset }; Act.Activities acts = new Act.Activities(); […]

如何在使用Json.NET进行序列化时应用重新映射所有属性名称的一般规则?

将Json对象反序列化为.Net type ,如果字段名称不匹配,我发现您可以使用[JsonProperty(PropertyName = “name”)]来装饰type的属性 这对于几个不匹配的属性来说很好,但是有没有办法设置约定或规则? JSON { “Job”: [ { “Job #”: “1”, “Job Type”: “A”, } ] } C# [JsonProperty(PropertyName = “Job Type”)] public string JobType { get; set; } [JsonProperty(PropertyName = “Job #”)] public string JobNumber { get; set; } 我有很多字段使用相似的名称,我想弄清楚,有没有办法告诉设置规则总是删除空格(EG: Job Type -> JobType )并用#替换# (例如: Job # -> JobNumber )? […]

使用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流(我无法控制),其forms如下: {“files”: { “/some_file_path.ext”: {“size”:”1000″, “data”:”xxx”, “data2″:”yyy”}, “/other_file_path.ext”: {“size”:”2000″, “data”:”xxx”, “data2″:”yyy”}, “/another_file_path.ext”: {“size”:”3000″, “data”:”xxx”, “data2″:”yyy”}, } } 所以,我有一个名为files的对象,它有许多属性,每次都有1)不同的名称,2)每次都有不同的数量,3)具有不能在C#属性中使用的字符的名称。 我该如何反序列化? 我把它放到一个可移植的库中,所以我不能在System.Web.Script.Serialization中使用JavaScriptSerializer,而且我不确定JSON.NET。 我希望使用标准的DataContractJsonSerializer。 更新:我已经将示例数据更改为更接近实际数据,并更正了不重要的区域中的JSON语法。 (仍然简化了很多,但其他部分相当标准)

C#JSON解析

我在修改一些JSON数据时遇到问题: { “title”: “LED SIDE DIRECTION INDICATOR 9-33V CATEGORY 6, 19cm CABLE CHROME BASE LED AUTO LAMPS”, “user_price”: { “inc_tax_value”: 34.067, “ex_tax_value”: 30.97, “base_inc_tax”: 34.067, “base_ex_tax”: 30.97, “currency_code”: “”, “price_rule”: “35” }, “local_quantity”: 0, “global_quantity”: 0, “url”: “http://ishop2.cooldrive.com.au/products/3102CM”, “all_warehouse_quantity”: 0, “description1”: “LED SIDE DIRECTION INDICATOR”, “description2”: “9-33V CATEGORY 6, 19cm CABLE”, “description3”: “CHROME BASE LED […]

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 { […]