JSON.Net – Change $type field to another name?

http://json.codeplex.com/workitem/22429 “I would rather keep $type hard coded and consistent.” Consistent with what I wonder? http://json.codeplex.com/workitem/21989 I would rather not – I think this is too specific to me and I don’t want to go overboard with settings. At some point I will probably implement this – http://json.codeplex.com/workitem/21856 – allowing people to read/write there own … Read more

Json.NET get nested jToken value

You can use SelectToken() to select a token from deep within the LINQ-to-JSON hierarchy for deserialization. In two lines: var token = jObj.SelectToken(“response.docs”); var su = token == null ? null : token.ToObject<Solr_User []>(); Or in one line, by conditionally deserializing a null JToken when the selected token is missing: var su = (jObj.SelectToken(“response.docs”) ?? … Read more

JObject.Parse modifies end of floating point values

@Ilija Dimov is correct–JSON.NET parses JSON floats as doubles by default. If you still want to use JObject instead of creating a full blown POCO for deserialization, you can use a JsonTextReader and set the FloatParseHandling option: var reader = new JsonTextReader(new StringReader(clientString)); reader.FloatParseHandling = FloatParseHandling.Decimal; JObject obj = JObject.Load(reader); Console.WriteLine(obj[“max”].Value<decimal>()); // 1214.704958677686

json.net – how to add property $type ONLY on root object

If you require the “$type” property on your root object and are OK with it appearing on nested polymorphic objects and arrays if required, use the following overload along with TypeNameHandling.Auto: JsonConvert.SerializeObject(Object, Type, JsonSerializerSettings). From the docs: public static string SerializeObject( Object value, Type type, JsonSerializerSettings settings ) type Type: System.Type The type of the … Read more

JSON.net ContractResolver vs. JsonConverter

Great question. I haven’t seen a clear piece of documentation that says when you should prefer to write a custom ContractResolver or a custom JsonConverter to solve a particular type of problem. They really do different things, but there is some overlap between what kinds of problems can be solved by each. I’ve written a … Read more

JSON.NET: Why Use JToken–ever?

From the standard, JSON is built out of the following five types of token: object: an unordered set of name/value pairs. array: an ordered collection of values. value: a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested. string number. … Read more

How To Deserialize Dynamic dirty json data in node js posted by react [duplicate]

Below shows you one approach to consider. The key is the use of the StudentDetails class to define the necessary properties of your individual students (as well as the Dictionary – so that it is keyed by the string (e.g. “John”)). using System; using System.Collections.Generic; using Newtonsoft.Json; namespace Sample { public class StudentDetails { public … Read more