http://stackoverflow.com/questions/38111298/json-net-deserialize-json-string-and-map-properties-to-different-property-names by Ismael

CSharp · Console

/Program.cs

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

public class Program
{
	public static void Main()
	{
		string json = @"
            {
                ""values"": {
                    ""details"": {
                        ""foo"": ""94"",
                        ""bar"": ""47""
                    },
                    count: 2
                }
            }";
		string json2 = @"
            {
                ""values"": {
                    ""details"": {
                        ""property1"": ""14"",
                        ""property2"": ""57""
                    },
                    count: 2
                }
            }";
		var root = JsonConvert.DeserializeObject<RootObject>(json);
		Console.WriteLine("property1: " + root.values.details.getProperty1);
		Console.WriteLine("property2: " + root.values.details.getProperty2);
		
		
		root = JsonConvert.DeserializeObject<RootObject>(json2);
		Console.WriteLine("property1: " + root.values.details.getProperty1);
		Console.WriteLine("property2: " + root.values.details.getProperty2);
	}
}

public class Details
{
	private string _property1;
	private string _property2;
	
	[JsonProperty("property1")]
	public string prop1
	{
		get;
		set;
	}
	

	[JsonProperty("foo")]
	public string foo
	{
		get;
		set;
	}

	public string getProperty1
	{
		get
		{
			_property1 = prop1 ?? foo;
			return _property1;
		}

		set
		{
			prop1 = value;
			foo = value;
		}
	}

	[JsonProperty("property2")]
	public string prop2
	{
		get;
		set;
	}

	[JsonProperty("bar")]
	public string bar
	{
		get;
		set;
	}

	public string getProperty2
	{
		get
		{
			_property2 = prop2 ?? bar;
			return _property2;
		}

		set
		{
			prop2 = value;
			bar = value;
		}
	}
}

public class Values
{
	public Details details
	{
		get;
		set;
	}

	public int count
	{
		get;
		set;
	}
}

public class RootObject
{
	public Values values
	{
		get;
		set;
	}
}
;