How to Configure JSON Format with Camel-Case Notation in ASP.NET MVC5
Today, I shall be demonstrating configuration of JSON format with camel case notation and removing extra slashes using ASP.NET REST Web API platform.
Prerequisites:
Following are some prerequisites before you proceed any further in this tutorial.- Knowledge of Camel Case Notation.
- Knowledge of Pascal Case Notation.
- Knowledge of REST Web API.
- Knowledge of ASP.NET MVC5.
- Knowledge of C# Programming.
Download Now!
Let's begin now.1) Create a new Web API project and name it "WebApiJsonFormat".
2) Install Json.NET - Newtonsoft via Nuget Package Manager.
3) Now, create your .NET JSON object mappers based on your JSON data either manually or via Json2Csharp online tool. I am using following JSON sample with .NET object mapper i.e.
Sample JSON Data
{ "fullname": "John Adam", "age": 30, "CarInfo": "I have big car" }
C#.NET Json Object Mapper
... public class RequestObj { // Gets or sets full name property. public string FullName { get; set; } // Gets or sets age property. public int Age { get; set; } // Gets or sets car info property. public string CarInfo { get; set; } } ...
Notice in the above sample JSON that property/key names do not have proper naming convention i.e. either camel case or pascal case notation. On the other hand the C#.NET object mapper class, the properties are in pascal notation i.e. first letter is capital, but, as we create our JSON response our requirements needed responding JSON in Camel case notation.
4) In next step create your web API method either POST or GET, I am making a simple HTTP POST method which after de-serializing the input JSON, serializes it back and send response.
... public HttpResponseMessage Post([FromBody] JToken postData) { // Initialization string jsonData = string.Empty; ... // Process code logic to create JSON response data. ... // Creating HTTP Response Message. HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(jsonData, Encoding.UTF8, "application/json"); // Info. return response; } ...
8) To fix camel case notation issue in my response JSON data, add following lines of code at the end of "Register(...)" method in "App_Start ->WebApiConfig.cs" file i.e.
... // If using JsonConvert for serialization. JsonConvert.DefaultSettings = () => new JsonSerializerSettings { Formatting = Formatting.Indented, ContractResolver = new CamelCasePropertyNamesContractResolver() }; ...
The above lines of code will be effective only whenever I use JsonConvert to serialize my JSON data object.
9) Now, upon executing the project, you can see that now my response JSON object in standard format with camel case notation according my business requirements i.e.
thanks for sharing amazing article..keep up
ReplyDeleteCadd centre fee structure in coimbatore | Cadd course in coimbatore | Autocad course in coimbatore | Solidworks course in coimbatore | 3dsmax course in coimbatore | Coreldraw course in coimbatore | Electrical Cad course in coimbatore | Best cad training centre in coimbatore | Ansys training in coimbatore | Catia training in coimbatore | Civilcad training in coimbatore | Cad software training course in coimbatore | Cad Project in Coimbatore
Much thanks to you for your blog article.Really anticipating read more. Will peruse on…
ReplyDeleteAmazid Shop
Interesting topic for a blog. I have been searching the Internet for fun and came upon your website. Fabulous post. Thanks a ton for sharing your knowledge! It is great to see that some people still put in an effort into managing their websites. I'll be sure to check back again real soon. Best Water Treatment Companies in UAE
ReplyDeleteanti hair fall shower filter
Sewage water treatment plant in Dubai UAE
Interesting subject matter for a blog. I have been searching the Internet for amusing and came upon your internet site. Fabulous submit. Thanks a ton for sharing your understanding! It is outstanding to peer that some people nevertheless put in an effort into dealing with their websites. Hot Water Repairs
ReplyDeleteThanks for sharing nice post
ReplyDeleteBest Regards:website maintenance consultant