C# 2.0 Json CamelCasePropertyNamesContractResolver

Jan 7, 2013 at 10:46 AM

Hi C# Experts,

I am using Newtonsoft Json.NET since my target .NET Framework is 2.0. I got an issue on deserialization:

Here's the Code Snippet:

using Newtonsoft.Json                                                                                                 using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//This is dynamic (Hard coded for example's sake)
string dynamicJSON = "{\"first_Name\":\"abc\",\"last_Name\":\"xyz\"}";

JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();

Person deserializedPerson = new Person();
deserializedPerson = JsonConvert.DeserializeObject<Person>(dynamicJSON, settings);

//null
//string fname = deserializedPerson.firstName; //???
}
}

public class Person
{
string fName;
string lName;

public string firstName
{
get
{
return fName;
}
set
{
fName = value;
}
}

public string lastName
{
get
{
return lName;
}
set
{
lName = value;
}
}
}

The code doesn't ignore underscore in "first_Name", which is dynamic JSON string , hence "deserializedPerson.firstName" is null

Please help.