Quick Inquiry

Home >> Articles >> How developers use JSON serialization and deserialization in asp.net mvc development

Json is a lightweight format that is used in asp.net development for data exchange. In this post, Aegis professionals will make you learn how to use JSON serialization and deserialization in asp.net MVC development project. In this post, experts are sharing steps to use NewtonsoftJson in mvc app. You can learn these simple steps and use the features accordingly.

We are introducing Json serialization and Deserialization using Newtonsoft.Json.dll in mvc based application.JSON (JavaScript Object Notation) is one lightweight data exchange format. Json is "name/value" assembly. Json structure is made up with {}, [], comma, colon and double quotation marks and it includes the following data types: Object, Number, Boolean, String, and Array.Serialize means convert an object instance to an XML document. Deserialize means convert an XML document into an object instance.

Below are the steps to use Newtonsoft Jsonin mvc based application.

1. Download the Latest release Newtonsoft Json dll, for download click here.

2. For give the reference ofNewtonsoft Json dll we need to add Reference form NuGet gallery as per below Screenshot.


3. We can also add the reference form Package manager console as per below screenshot.


Now here Json Serialization is the process of converting the state of an object into a form that can be persisted in a storage medium or transported across the processes or machines. Same way deserialization which is a process that converts the outcome of serialization into the original object.After completing the package installation for Newtonsoft Json we can Serialize and deserialize data as per below code.Give the reference to the code by using Newtonsoft.Json namespace in code.

For Serialize the Json object we have to write the below code,

String __sJsonOut = String.Empty; stringmailaccount = EmailAccount; RootObject __oServerSettingParameter = newRootObject(); __oServerSettingParameter.id = mailaccount; Configuration _oServerSettingConfig = newConfiguration(); _oServerSettingConfig.password = Password; _oServerSettingConfig.type = Type; _oServerSettingConfig.port = int.Parse(Port); _oServerSettingConfig.mailserver = MailServer; _oServerSettingConfig.ssl = SSL; _oServerSettingConfig.username = Uname; __oServerSettingParameter.configuration = _oServerSettingConfig; __sJsonOut = JsonConvert.SerializeObject(__oServerSettingParameter);

Here, we have two Object class RootObject and configuration class In which we have to Serialize the Json string.

Result of the Serialize Object:

1. Serialization:-


Above Screenshot is for Result of the Serialize the object in String. Using JsonConvert.SerializeObject we have convert object into a String. Now same way we can deserialize the Json string into the object as per below code.

List<RootObject>lstRootObject = newList<RootObject>(); foreach (AutoEventActionitemserverSettinginlstServersettings) { stringsTemplate = itemserverSetting.Parameters; RootObjectConfigmodel = JsonConvert.DeserializeObject<RootObject>(sTemplate); lstRootObject.Add(Configmodel); }

Here, in above code we can deserialize the Json string onto Objcet. For that we have required the object class for Deserialize, In our case we have create RootObject class for Dserialize the Json string as per below.

publicclassConfiguration { publicstring type { get; set; } publicstringmailserver { get; set; } publicstring username { get; set; } publicstring password { get; set; } publicint port { get; set; } publicboolssl { get; set; } } publicclassRootObject { publicstring id { get; set; } publicConfigurationconfiguration { get; set; } }

So, as per above code we get the Deserialize Result in above Object class. Also we can give settings in JsonCovert Serialize and deserialize methods like below.

serverSettings = JsonConvert.DeserializeObject<RootObject>(Jsonstring, newJsonSerializerSettings() { Error = (sender, args) =>args.ErrorContext.Handled = true });

So, as per above code we can handle the Error during the Serialize or Deserialize the object.

Advantages Serialization - Deserialization:-

1. The main benefit is the speed in populating an object as opposed to querying multiple tables in the case of database storage.
2. Simplicity in coding for serialization and deserialization is another benefit.

Hope you can now use Json serialization and deserialization in asp.net mvc development and design intuitive application. Aegis experts have also shared advantages of serialization and deserialization. You can make comments and ask asp.net development related queries from Aegis professionals.


For further information, mail us at info@aegissofttech.com


Copyright © 2017 - aegisisc, All rights reserved