json.net

Json.net deseralize to a list of objects in c# .net 2.0

本小妞迷上赌 提交于 2019-12-06 01:19:39
问题 I'm trying to deseralize some json into a collection (list), but I'm not sure which method will return a list of objects, or do I have to loop through something and copy it to my own list? Can anyone tell me the syntax or method I should use for this. I've created my object with some properties, so it's ready to be used to hold the data. (title,url,description) I've tried this, but it doesn't seem quite right List<newsItem> test = (List<newsItem>)JsonConvert.DeserializeObject(Fulltext); 回答1:

How can I deserialize a child instance as a parent object without losing its specific property?

别说谁变了你拦得住时间么 提交于 2019-12-06 01:19:22
Below are several classes. How can I serialize a Json string of an instance of A in which PropertyB contains either SpecPropB1 or SpecPropB2 so that those properties remains in the C# object? public class A { public B PropertyB {get;set;} } public class B { public string GenProp {get;set;} } public class B1:B { public string SpecPropB1 {get;set;} } public class B2:B { public string SpecPropB2 {get;set;} } I see multiple solutions to this problem but they lack elegance: Put the type of propertyB as object: I lose the strong typing for this property. Redeserialize the problematic part as a child

How to prevent Json.NET converting enum to string?

五迷三道 提交于 2019-12-06 01:06:49
The following class public class RequestSections : RequestBase { public RequestSections(Command c, Dictionary<SectionIdentifier, BigInteger> v) : base(c) { VERSIONS = v; } public Dictionary<SectionIdentifier, BigInteger> VERSIONS { get; set; } } is serialized to JSON using JSON.NET and producing the following JSON output: { "VERSIONS": { "Photos": 901, "Music": 902 }, "CMD": 43 } The problem is that SectionIdentifier is enum but JSON.NET converts them to string. public enum SectionIdentifier { Photos = 1000, Music } How can I prevent JSON.NET converting integer enum values to string? I would

How to set Json.NET ContractSerializer for a certain specific type instead of globally?

蓝咒 提交于 2019-12-06 00:59:57
问题 I want to set a contract serializer just for certain types in my ASP.NET Web API application. I can set the settings globally in the App_Start/FormatterConfig.cs like this: public static void RegisterGlobalFormatters(MediaTypeFormatterCollection formatters) { jsonSerializerSettings.ContractResolver = new CriteriaContractResolver(new List<string>(new string[]{"mdData", "name", "label"})); ... but how can I just apply this to one or more specific class types? The reason I want to do this is

Custom inheritance JsonConverter fails when JsonConverterAttribute is used

风流意气都作罢 提交于 2019-12-06 00:54:32
I am trying to deserialize derived type, and I want to use a custom property Type to distinguish between derived types. [ { "Type": "a", "Height": 100 }, { "Type": "b", "Name": "Joe" } ] The solution I came to was to create a custom JsonConverter . On ReadJson I read the Type property and instantiate that type through the ToObject<T> function. Everything works fine until I use a JsonConverterAttribute . The ReadJson method loops infinitely because the attribute is applied on subtypes too. How do I prevent this attribute from being applied to the subtypes? [JsonConverter(typeof(TypeSerializer))

Serialize custom properties on a class that implements IEnumerable [duplicate]

南笙酒味 提交于 2019-12-06 00:47:57
问题 This question already has answers here : How do I get json.net to serialize members of a class deriving from List<T>? (3 answers) Closed 3 years ago . Currently, JSON.NET ignores all other properties on classes implementing IEnumerable and serializes the array. How can I tell JSON.NET to serialize the custom properties? I'm trying to serialize the PagedList<T> implementation below: public interface IPagedList : IEnumerable { int PageIndex { get; } int PageSize { get; } int TotalCount { get; }

Is it possible to return a pre-encoded JSON string in a SignalR hub method?

我是研究僧i 提交于 2019-12-06 00:47:14
In an MVC project, I have a method on a hub similar to this: public string Foo() { return DoCrazyThingThatReturnsJson(); } Unfortunately SignalR (or something) takes the encoded JSON string and happily encodes it, then returns it, so browser be like LOLWTF. Is there a way to skip this second encoding? Looking at this here : We assume that any ArraySegment is already JSON serialized it seems like something like this may work (note that I haven't tried it, so no promises): string jsonString = ...; // your serialized data var jsonBytes = new ArraySegment<byte>(Encoding.UTF8.GetBytes(jsonString));

Json.Net deserialize JSON objects with index as name [duplicate]

爷,独闯天下 提交于 2019-12-06 00:35:47
This question already has an answer here: How can I parse a JSON string that would cause illegal C# identifiers? 2 answers I am attempting to parse JSON from a web service using Json.NET, the web service returns data in the following format: { "0": { "ID": 193, "Title": "Title 193", "Description": "Description 193", "Order": 5, "Hyperlink": "http://someurl.com" }, "1": { "ID": 228, "Title": "Title 228", "Description": "Description 228", "Order": 4, "Hyperlink": "http://someurl.com" }, "2": { "ID": 234, "Title": "Title 234", "Description": "Description 234", "Order": 3, "Hyperlink": "http:/

Newtonsoft.json IsoDateTimeConverter and DateFormatHandling

我的梦境 提交于 2019-12-05 22:50:35
This is not an issue but concept I just want to make it clear. When working with asp.net web api we use Newtonsoft.Json's SerializerSettings . In this setting we can set jsonSetting.Converters.Add(new IsoDateTimeConverter()); jsonSetting.DateFormatHandling = DateFormatHandling.IsoDateFormat; I understood what IsoDateTimeConverter is doing but but not clear about DateFormatHandling.IsoDateFormat . It seems to be there is no effect of DateFormatHandling.MicrosoftDateFormat if you still using IsoDateTimeConverter . Still excepting some explanation who know about this. dbc You actually have

Json.NET under Unity3d for iOS

别来无恙 提交于 2019-12-05 22:49:33
问题 has anyone succeeded to adapt/port Json.NET to a version able to run under Unity3d deployed to iOS (and the webplayer, and android, too, but these seem less problematic), somehow overcoming the AOT issues there? Or are there plans to release a compatible version of Json.NET? Many thanks, Max 回答1: Newtonsoft Json fails AOT in iOS and also fails at deserializing anything inheriting from KeyedCollection in the webplayer. Here's a version that has been fixed: JSON .NET For Unity (Supports iOS)