1

I tried using

var myobject = JsonConvert.SerializeObject(Customer);

but problem is in Customer properties are like

FirstName and my service expecting json input like firstName {"firstName":"Neo"}

statement JsonConvert.SerializeObject(Customer); gives me {"FirstName":"Neo"} which is wrong.

So How can I change first letter when JsonConvert.SerializeObject happened ?

Or How to take only one parameter as input json firstname instead if using Customer object.

3 Answers 3

2

You should use the Json.NET attribute support to customize the naming:

public class Customer
{
    [JsonProperty("firstName")]
    public string FirstName { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can define how data need to be serialized. When using webapi, we can define a CamelCasePropertyNamesContractResolver (part of json.net library) as formatter in the register method of the webapi config.

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    { 
       config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();            
    }
}

The code above is especially for webapi, nevertheless I believe a simular approach can be a solution when not using webapi.

Comments

1

You can use like this. Use DataMember property, it'll serailize as mentioned.

[DataContract(Namespace = "")]
public class Customer 
{
    [DataMember(Name = "firstName")]
    public string FirstName { get; set; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.