json.net

How to disable TypeNameHandling when specified in attributes by using JsonSerializerSettings in Json.NET?

假如想象 提交于 2019-12-06 11:34:55
Sometimes I need to suppress output of "$type" properties by Json.NET even when specified by JsonPropertyAttribute.ItemTypeNameHandling . How can this be done? My root class looks like below: public class DomainResource { [JsonProperty(ItemTypeNameHandling = TypeNameHandling.Auto)] public List<Extension> Extensions { get; set; } } And in addition I have a class hierarchy for Extension such as the following: public class Extension { readonly string url; public string Url { get { return url; } } public Extension(string url) { this.url = url; } } public class IntegerExtension : Extension { public

Deserializing some JSON with JSON.NET

空扰寡人 提交于 2019-12-06 10:18:04
问题 I'm very new to JSON, and I need to parse some that an API is providing. A quick google search turned up JSON.NET, so I'm trying to use it now to parse this JSON into a list object. First of all, is JSON.NET the best library to use for this? This is what I'm trying to do: I have a class called Item , for example. The json has many "elements" (if that's what they are called), and each contains 3 fields : an integer named id, a string named name, and a datetime named creationTime. I would like

Unexpected result from call to Newtonsoft.Json from F#

浪子不回头ぞ 提交于 2019-12-06 10:15:01
I am not getting the expected result from this F# code. I would expect t to contain values as a result of the call to JsonSchema.Parse(json) but instead it is empty. What am I doing wrong? open Newtonsoft.Json open Newtonsoft.Json.Schema let json = """{ "Name": "Bad Boys", "ReleaseDate": "1995-4-7T00:00:00", "Genres": [ "Action", "Comedy" ] }""" [<EntryPoint>] let main argv = let t = JsonSchema.Parse(json) 0 // return an integer exit code Mark Seemann As John Palmer points out, JsonSchema.Parse parses a JSON schema , but from your question, it looks as though you want to parse a normal JSON

How to access nested object from JSON with Json.NET in C#

本小妞迷上赌 提交于 2019-12-06 10:11:47
问题 How can I select the json value "estimatedLocationDate" within a nested object using class decoration? The property "estimatedLocationDate" always returns null instead of the value 2015-10-01T14:00:00.000 . The other decorated values return the proper values. Here is my C# class public string id { get; set; } public string name { get; set; } [JsonProperty("publishedDate")] public string publishdate { get; set; } [JsonProperty("estimatedLocationDate")] public string estimatedLocationDate{ get;

JToken is not a reference of JObject?

匆匆过客 提交于 2019-12-06 10:07:22
问题 I have not yet noticed that James Newton King wrote or spoke about what JToken is . I have made the incorrect assumption that it somehow holds a reference to JObject . This is not the case as the these LINQPad statements demonstrate: var json = @" { ""item"": { ""foo"": ""4"", ""bar"": ""42"" } } "; var jO = JObject.Parse(json); var jToken = jO["item"]["foo"]; jToken = "5"; jO.ToString().Dump("jO"); jToken.Dump("jToken"); The output: jO { "item": { "foo": "4", "bar": "42" } } jToken 5 Should

How to recursively deserialize dynamic JSON into .NET objects using Json.NET on windows phone

强颜欢笑 提交于 2019-12-06 10:02:19
I am receiving some dynamic JSON in a windows phone app and I need to be able to completely deserialize it into objects that can be saved into isolated storage. For this application it would be best to avoid becoming locked into a class structure because I cannot predict exactly what type of JSON I will be getting back from a server. Here is an example of how I am using Json.NET to deserialize: JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString); In this example the Values held in the dictionary may be Lists or Dictionaries or Strings. The problem with this approach is that

Removing NaN values in deserialization Web API 2 C#

喜欢而已 提交于 2019-12-06 09:59:24
Hello I am wondering if anyone could help me, I am trying to automatically replace NaN from double values automatically with 0 upon deserializing automatically in Web API 2. I am trying to use JSON.NET but I am having no success. Any help would be greatly appreciated. I am placing the below into my WebApiConfig config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); MediaTypeHeaderValue appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml"); config.Formatters.XmlFormatter.SupportedMediaTypes

C# 使用Json.NET对数据进行序列化和反序列化

时间秒杀一切 提交于 2019-12-06 09:59:14
本文首发于个人博客 https://kezunlin.me/post/22391aa3/ ,欢迎阅读最新内容! c# json serialize and deserialize using json.net JsonConvert <!--more--> Guide Json.NET JsonConvert.SerializeObject JsonConvert.DeserializeObject install Use NuGet to download the package "Project" -> "Manage NuGet packages" -> "Search for "newtonsoft json". -> click "install". code reference using Newtonsoft.Json; serialize collections Product p1 = new Product { Name = "Product 1", Price = 99.95m, ExpiryDate = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc), }; Product p2 = new Product { Name = "Product 2", Price = 12.50m,

AssemblyLoadException in PostSharp on release build

吃可爱长大的小学妹 提交于 2019-12-06 09:43:36
I have a couple of projects that all include the json.net 6.0.3 NuGet package. When I add: using Newtonsoft.Json; I get the following error when building: 1>F:\TwinkyTalk\TwinkyTalk.csproj : error PS0099: Unhandled exception (3.1.48.0, 32 bit, CLR 4.5, Release): PostSharp.Sdk.CodeModel.AssemblyLoadException: Cannot find assembly 'newtonsoft.json, version=4.5.0.0, culture=neutral, publickeytoken=30ad4fe6b2a6aeed'. [Version mismatch] 1>F:\TwinkyTalk\TwinkyTalk.csproj : error PS0099: ============ PostSharp Assembly Loading Log =================== 1>F:\TwinkyTalk\TwinkyTalk.csproj : error PS0099:

Cannot deserialize xml string with Newtonsoft.Json.JsonConvert.DeserializeObject

依然范特西╮ 提交于 2019-12-06 09:37:07
问题 Hi I am passing an xml as string <AbcDto xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Abc"> <Id>2</Id> <Description>sample string 4</Description> <Name>sample string 3</Name> <PolicyId>c17f5b9f-c9bf-4a3a-b09b-f44ec84b0d00</PolicyId> <Status>Active</Status> <TimeZoneId>USCentral</TimeZoneId> </AbcDto> When I am trying creating Custom Model Binder for Web Api public bool BindModel(System.Web.Http.Controllers.HttpActionContext actionContext,