How to return JSon object

First of all, there’s no such thing as a JSON object. What you’ve got in your question is a JavaScript object literal (see here for a great discussion on the difference). Here’s how you would go about serializing what you’ve got to JSON though: I would use an anonymous type filled with your results type: … Read more

Deserialize array of key value pairs using Json.NET

The simplest way is deserialize array of key-value pairs to IDictionary<string, string>: public class SomeData { public string Id { get; set; } public IEnumerable<IDictionary<string, string>> Data { get; set; } } private static void Main(string[] args) { var json = “{ \”id\”: \”123\”, \”data\”: [ { \”key1\”: \”val1\” }, { \”key2\” : \”val2\” } … Read more

Using, how do I prevent serializing properties of a derived class, when used in a base class context?

I use a custom Contract Resolver to limit which of my properties to serialize. This might point you in the right direction. e.g. /// <summary> /// serializes ALL properties of a class by default /// this class will tell to only serialize properties if they MATCH /// the list of valid columns passed … Read more

Convert an int to bool with Json.Net [duplicate]

Ended up creating a converter: public class BoolConverter : JsonConverter { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { writer.WriteValue(((bool)value) ? 1 : 0); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { return reader.Value.ToString() == “1”; } public override bool CanConvert(Type objectType) { return objectType == typeof(bool); } … Read more

How to access elements of a JArray (or iterate over them)

There is a much simpler solution for that. Just treat the items of the JArray as JObject. Let’s say we have such array of JSON objects: JArray jArray = JArray.Parse(@”[ { “”name””: “”Croke Park II””, “”url””: “”″”, “”promoted_content””: null, “”query””: “”%22Croke+Park+II%22″”, “”events””: null }, { “”name””: “”Siptu””, “”url””: “”””, “”promoted_content””: null, “”query””: “”Siptu””, “”events””: null … Read more

Ignoring null fields in

Yes you need to use JsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore. But because structs are value types you need to mark Field2, Field3 nullable to get the expected result: public struct structA { public string Field1; public structB? Field2; public structB? Field3; } Or just use classes instead of structs. Documentation: NullValueHandling Enumeration

Default value for missing properties with

I found the answer, just need to add the following attribute as well: [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)] In your example: class Cat { public Cat(string name, int age) { Name = name; Age = age; } public string Name { get; private set; } [DefaultValue(5)] [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)] public int Age { get; private set; } … Read more