How to get camelCase property name using Newtonsoft JSON serializer?

Posted by Sheonarayan under Error and Solution on 1/30/2014 | Points: 10 | Views : 7052 | Status : [Administrator] | Replies : 0

This is a strange behaviour of NewtonSoft serializer. By default when you serialize using JsonConvert.SerializeObject, it gives property name as Title case.

To convert Title case property to camelCase, we can use following code snippet.

Use below namespaces

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

var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
return JsonConvert.SerializeObject(courses, Formatting.None, settings);

Notice that "courses" is my collection of objects here.

Hope this will be helpful.

Sheo Narayan


(No response found.)

Login to post response