json.net

How to deserialize objects containing arrays of values with a fixed schema to strongly typed data classes?

给你一囗甜甜゛ 提交于 2019-12-24 07:49:09
问题 I am having some trouble figuring out a clean (as possible) way to deserialize some JSON data in a particular format. I want to deserialize the data to strongly typed data object classes, pretty flexible regarding the specifics of this. Here is an example of what the data looks like: { "timestamp": 1473730993, "total_players": 945, "max_score": 8961474, "players": { "Player1Username": [ 121, "somestring", 679900, 5, 4497, "anotherString", "thirdString", "fourthString", 123, 22,

ASP.Net Core 2.2 - separate serializer settings for input and output

流过昼夜 提交于 2019-12-24 07:31:03
问题 ASP.Net Core 2.2 allows to set serializer settings using MvcJsonOptions.SerializerSettings property. The problem is that it affects both input and output. Is there a way to have separate options for input (deserialization) and output (serialization)? In particular, I need to set a different behavior for NullValueHandling settings: ignore null errors for non-nullable fields when deserializing client json but keep nulls for defined model fields when serializing the result. For example, I have a

Ignoring an invalid field when deserializing json in Json.Net

不问归期 提交于 2019-12-24 07:16:50
问题 I'm trying to deserialize some json from a 3rd party provider, and occasionally it returns some invalid date field (like -0001-01-01 or something). This causes the process to throw an exception. Is there a way to tell Json.Net to ignore fields that are invalid? Thanks Matt 回答1: To expand on the answer from David, I have used a custom DateTime converter: public class SafeDateTimeConvertor : DateTimeConverterBase { public override object ReadJson(JsonReader reader, Type objectType, object

How to parse JSON Response into Dictionary?

半城伤御伤魂 提交于 2019-12-24 06:35:17
问题 I have a JSON resonse (HTTPWebResponse) coming back from my WCF Service. It looks like this : { "New SessionResult": [ { "Key": "Token", "Value": "token_value" } ] } I am using JSON.NET (http://james.newtonking.com) and I cant figure out how to deserialize this response into a Dictionary<string, string>. I have tried several things but each one says in different wording "cannot convert JSON into sting". Currently I tried this : Dictionary<string, string> values = JsonConvert.DeserializeObject

Deserialize JSON object in c# using Newtonsoft json.net

最后都变了- 提交于 2019-12-24 06:07:30
问题 I have JSON array. I want to map/deserialize this into following class. public sealed class DataItem{ public string UserId {get; set;} public string TestId {get; set;} public string UserName{get; set;} public string Data1 {get; set;} public string Data2 {get; set;} public string Data3 {get; set;} public string Data4 {get; set;} public string Data5 {get; set;} public string Data6 {get; set;} public string Data7 {get; set;} public string Data8 {get; set;} public string Data9 {get; set;} public

Deserialize JSON object in c# using Newtonsoft json.net

倖福魔咒の 提交于 2019-12-24 06:07:06
问题 I have JSON array. I want to map/deserialize this into following class. public sealed class DataItem{ public string UserId {get; set;} public string TestId {get; set;} public string UserName{get; set;} public string Data1 {get; set;} public string Data2 {get; set;} public string Data3 {get; set;} public string Data4 {get; set;} public string Data5 {get; set;} public string Data6 {get; set;} public string Data7 {get; set;} public string Data8 {get; set;} public string Data9 {get; set;} public

Json.Net Is converting on its own before using my JsonConverter

三世轮回 提交于 2019-12-24 05:59:59
问题 In my WPF code, I'm using Newtonsoft.Json to deserialize json into my models. First, I receive a Json string ('json') which I then parse into 'message'. (The object I want to deserialize is wrapped in a "data" field in the json string). Activity message = JObject.Parse(json)["data"].ToObject<Activity>(); My Activity class uses several [JsonProperty] attributes to generate its fields. One of them is an enum called 'ActivityType'. [JsonProperty("type")] [JsonConverter(typeof

How to pass DefaultValue for custom class when deserializing with Json.net

ぃ、小莉子 提交于 2019-12-24 05:55:17
问题 I want to deserialize a JSON structure like this: { "Name": "Foo", "bar": { "Name": "Bar", "DefaultableProperty": "default" } } ... where it is optional to define "bar" . And in the case that it is not defined, I want to load an object of type Bar with some default values. Here are my class definitions for Foo and Bar public class Foo { [JsonConstructor] public Foo() { string bardefaults = @"{ ""Name"": ""defaultname""}"; b = new Bar(bardefaults); } public string Name { get; set; } public Bar

Serializing and deserializing TimeSpan values to a property of type Object

南楼画角 提交于 2019-12-24 05:42:17
问题 I have a class that has a field of type Object that may contain a TimeSpan object. I am serializing this and the deserializing this: public class MyObject { public object myTimeSpan { get; set; } } ... var myObject = new MyObject { myTimeSpan = TimeSpan.FromDays(2) }; var json = JsonConvert.SerializeObject(myObject); ... var duplicateObject = JsonConvert.DeserializeObject<MyObject>(json); And when I do, duplicateObject.myTimeSpan contains the string "2:00:00". How can I get this to

Sorting a JSON Object using Linq

本小妞迷上赌 提交于 2019-12-24 05:09:05
问题 I am getting a response back from a Google Search Appliance suggest service in the form of JSON in the following format string jsonString = @"{ ""query"": ""t"", ""results"": [ { ""name"": ""tom"", ""type"": ""suggest"" }, { ""name"": ""tim"", ""type"": ""suggest"" }]}"; I want to sort the results list by name alphabeticaly and change the names to sentence case. I can do this in jquery but would prefer to do it on the server side for performance reasons. I can sort the results but that