Can I specify a path in an attribute to map a property in my class to a child property in my JSON?

Well, if you just need a single extra property, one simple approach is to parse your JSON to a JObject, use ToObject() to populate your class from the JObject, and then use SelectToken() to pull in the extra property. So, assuming your class looked something like this: class Person { [JsonProperty(“name”)] public string Name { … Read more

How do I write a custom JSON deserializer for Gson?

I’d take a slightly different approach as follows, so as to minimize “manual” parsing in my code, as unnecessarily doing otherwise somewhat defeats the purpose of why I’d use an API like Gson in the first place. // output: // [User: id=1, name=Jonas, updateDate=2011-03-24 03:35:00.226] // [User: id=5, name=Test, updateDate=2011-05-07 08:31:38.024] // using java.sql.Timestamp public … Read more

Deserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?)

You can use the C# dynamic type to make things easier. This technique also makes re-factoring simpler as it does not rely on magic-strings. JSON The JSON string below is a simple response from an HTTP API call, and it defines two properties: Id and Name. {“Id”: 1, “Name”: “biofractal”} C# Use JsonConvert.DeserializeObject<dynamic>() to deserialize … Read more

.NET NewtonSoft JSON deserialize map to a different property name

Json.NET – Newtonsoft has a JsonPropertyAttribute which allows you to specify the name of a JSON property, so your code should be: public class TeamScore { [JsonProperty(“eighty_min_score”)] public string EightyMinScore { get; set; } [JsonProperty(“home_or_away”)] public string HomeOrAway { get; set; } [JsonProperty(“score “)] public string Score { get; set; } [JsonProperty(“team_id”)] public string TeamId … Read more

How do I use JSON.NET to deserialize into nested/recursive Dictionary and List?

If you just want a generic method that can handle any arbitrary JSON and convert it into a nested structure of regular .NET types (primitives, Lists and Dictionaries), you can use JSON.Net’s LINQ-to-JSON API to do it: using System.Linq; using Newtonsoft.Json.Linq; public static class JsonHelper { public static object Deserialize(string json) { return ToObject(JToken.Parse(json)); } … Read more

How can I parse JSON with C#?

As was answered here – Deserialize JSON into C# dynamic object? It’s pretty simple using Json.NET: dynamic stuff = JsonConvert.DeserializeObject(“{ ‘Name’: ‘Jon Smith’, ‘Address’: { ‘City’: ‘New York’, ‘State’: ‘NY’ }, ‘Age’: 42 }”); string name = stuff.Name; string address = stuff.Address.City; Or using Newtonsoft.Json.Linq : dynamic stuff = JObject.Parse(“{ ‘Name’: ‘Jon Smith’, ‘Address’: { … Read more

tech