Serialize and Deserialize Json to C# [Step By Step Tutorial of JSON in C#]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends welcome to coding drop let's see sharp tutorial so in this video we are going to see how we can serialize d serialize json string and json objects so uh also we are going to see what are uh some kind of attributes that we can use along with the c-sharp classes in order to serialize and deserialize json objects so please subscribe the channel if you have not subscribed it yet and don't forget to press the bell icon so you will get notified once we upload new videos and please like the video if you are enjoying it so let us start the session now so here now i have opened the sample application this is the symbol console application so in this application we will be showing all about json serialization deserialization all those stuffs okay so now what i'm going to do is i'm going to install one library from nougat packages so i'm just choosing manage look at packages and so you can see now there are no libraries installed in this application so i'm just searching for newton's soft oh okay it is done in the list itself so okay newtonsoft.json this is a very famous library which we can use to manage json objects okay so it is getting installed in the application yeah now it got installed so i'm just changing the tab to install so you can see now that library got installed fine so now showing the demo uh first i'm creating a class private class say customer okay so i'm just giving an attribute name serializable here and inside the customer we are having a string customer name then we have a string uh email [Music] then now we have two strings here okay let us have an integer variable so hint age okay so now that is also fine and another variable what we can use is decimal okay but what variable we can use okay anyway this is for a demo purpose so i'm just using total sales so just assume that here we are showing the total uh the amount of total sales done by this customer okay so now we have string in description that is a daytime [Music] uh final invoice all uh okay i'll do one thing final purchase date so uh just we are assuming this variable shows the date uh on which the customers have a customer have made a purchase okay fine so we have an object here or we have created a class here customer okay now uh we are creating a list of customers so i'm naming it as customers is equal to new list of customer fine so it is having a new customer i'm just inserting some values into it so the first customer name is john or sorry and his age is um 33 this email address is john adriate say [Music] yeah okay gmail.com okay fine now total sales done by this customer so this is a decimal variable so for total sales i am giving some value say four thousand okay now final purchase date i'm creating a new date uh object so new date of 2021 and today is 2021 march 29 so okay i'm giving the same date here fine so now we have one customer here and just copying it and creating another one so the name of the second customer is george and here his age is 32 for example so uh his email address used george gmail.com and his total price is sorry total sales is 6 000 and final purchase date is okay february uh 12 okay five now i'm creating one more customer so his name is peter and his age is some okay 28 and his email id is peter gmail.com and his total sales is 2000 and final purchase date i'm doing it as january [Music] 10th okay so now we have sorry yeah now we have three customers in our customer list so this is a collection of customers in which the three different customer data is there now i need to convert this data into a json string so for that what i'm doing is i'm declaring a new variable customer json is equal to now here we can use json convert okay so now it will show an error so what we can do is if we click here it will show that we have to use newtonsoft.json so i'm just keeping that okay json.convo dot serialize object off and here i'm just providing the customer list okay now in uh in the console application i'm just printing the value of customer json so customer console.writeland.com okay of customer.json and finally i'm giving consume console.read key okay so i'm running the application now now let's see how we are getting the json format so you can see here uh i'll do one thing have you copy this and paste it in a notepad okay so this is how now the value is coming so here we can see it is a list or an array okay inside that we have multiple records so the first one is customer name john email and age everything has came here and see the date format so the date has been formatted like this okay so uh in the same way uh the second code and the third record has been uh converted to a json so completely it is showing in the json format okay this is how we can convert an object to a json string okay fine now i'm just copying this sorry i'm just closing that now so now we got how we can convert an object or a list to a json format now i will show you one more thing say here i'm having one single data where customer one is equal to new customer of and here i'm just copying the values from here okay and instead of peter i'm giving some other name uh say what name i can give [Music] james okay and the age is 20. james gmail.com and total sales is some 1500 sorry yeah and final purchase did i'm giving it as february 15th okay now i'm just converting this particular object to json so previously i have shown how we can convert a list of objects now i am just converting a single object to a json now i will show you how it works so i'm running the application so now you can see it will show like this and just copy this to the notepad yes so here you can see the object is a json string so this is how we can convert an object or a list fine hope you got the point now let me go to this deserializing json string okay fine so in order to show that what i'm doing is i'm just okay i'm not removing this let it be there you know to show the demo now first um okay i'm just serializing this here okay uh i'm just creating a big point here and what i need to do is i need to copy that json string and now we are going to deserialize this json string so i'm just opening notepad and copying it here okay fine now leave all these things we don't need this so war [Music] or json string is equal to now we are going to paste the json string here so here we know that we have a double digit comma here so we need to escape that so in order to escape that um using a slash before this double inverted comma now i'm replacing all the double inverted uh sorry the double quotes with the uh slash double quote okay fine so that is done now now copying it and pasting it here so you can see now there is a string that is a json string okay now we don't need this this is for serializing that now what i'm doing here is you are uh i'm naming it as customer list yeah i think we already have customer list so they need that customer list 2 is equal to json convert dot d serialize object now here we can mention the type of the d serialized object so here we know this json string will return a list of customers okay so what we can do is the data type is list of customer okay and here i'm giving json string file now let us check whether we are receiving the list of customers in this customer list two variables so i'm running the application now sorry some error happened somewhere i think oh sorry it was here actually so here customer json okay we don't need this file now let us check how it will return the values so i have created a breakpoint here fine now we are going to check what is the in the customer list so we can see that there are three objects in customer list so all are the customer type and here we can see we have this uh we got the same data what we have in our json string okay so here the first data the first customer is john john gmail.com 4000 is the total sales and age is 33 and even the day time is 29 march 2021 so we got the exact deserialized values here so you can see now this is of george and this is of peter so everything works fine in the same way if we uh if we are using an ins if we are using a single object listed realizing a single object and we are using the same json string here it will convert to a single object i will show you just now so what customer js is equal to json i'm just serializing this object customer one okay uh just for copying the value or copying the json string [Music] okay so now i'm copying this from here fine and we don't need this now so i'm pasting it here okay now i'm removing uh this customer json what we have created now now instead of this okay i have removed it completely now the json string what we need is the new one which we have copied so i'm just replacing the double quotes with slash double quotes so it will escape the double quotes from the string okay fine now i'm pasting it here so now uh the json string is a single customer record and here we are deserializing with type list of customer but we know this is not a list of customer this is a single customer so we don't need list here and we'll be removing it completely so now json convert dot d serialize object of customer of json stream okay fine now let us check by running the application so see here now uh sorry yeah customer list see uh we got a single customer object and it is having the same values what we have in our json string so it is completely deserialized and we got the values here file now we are going to see an attribute that we can use in the model class for formatting or we can say for giving some custom names in the json string so this newton soft.json is uh in that library we can use an attribute named json property okay so this comes under newtons of newtonsoft.json so here what i can do is i can give a property name i will show you now so instead of customer name what i'm doing is i'm giving it like this customer underscore name okay and instead of email i'm using email completely in lowercase and for age i'm using age incompletely in our case instead of total sales giving it like this and for final purchase date final purchase date file okay now let us remove all this first time serializing the list oh sorry uh okay so here we are serializing the customers customer list here okay customers and let us see how the json value is coming i'm just creating a breakpoint here so we can copy to the notepad okay so i'm copying it here copying it from here and pasting it in the notepad now we can see instead of our object customer name now it is coming as customer underscore name whatever the jc property what we have given here so while serializing the library will use the json property value or the property name what we have used here in order to serialize this particular element so here in email it has used email completely in our case and here you can see purchase date total sales everything it has used adjacent property value what we have used now while deserializing i will show you that as well so i'm just uh replacing the double cubes fine now i'm popping this in order to show you the dc realization method fine now now i'm declaring available here json string is equal to okay i have pasted it here so now you can see now what we have pasted is it contains customer underscore name email uh then age total sales everything is different from the normal element names okay now we are going to deserialize this value of this json string so i'm just removing this and var customer list 2 is equal to json convert dot d serialize object then i am giving the data type is list of customer file of json stream file now let us check whether we are getting the values correctly i am running the application it has not drawn any error messages and you can see it has written three customer elements or three customer objects and here we have received same customer name age email and all those details in the same object okay so here it all it automatically converted customer underscore name to customer name and all in all objects it has returned in the same way so while deserialization also this library will auto convert json property this value so in the json string we should have the json property value as the field names and while deserializing it will get auto converted to the normal objects so this is how we can use serialization and d serialization using json convert or newtons of json library and here we can read the json string from different methods now i have just hard coded it here but if you need to read it from a file uh or from anywhere you can do that if you read it from a file you can just use um or i'll just show you now json string is equal to file file will be under system.io namespace file dot read all text then you can just provide the file path here so if you if you have this json string in a file automatically you can get the complete json string in this variable using file dot read all text here and you can convert into a normal class object so hope you got the logic if you have any doubts please don't hesitate to comment to the video we will be replying it as soon as possible and also you can comment your feedbacks we are glad to hear from you so subscribe the channel and press the bell icon so that you will get notified when we upload new videos and hope you enjoyed the video please like the video see you in the next video thank you all
Info
Channel: Coding Droplets
Views: 45,848
Rating: undefined out of 5
Keywords: Creating json object in c#, c#, c# class to json, c# json, c# json parser, c# json to object, c# to json, datatable to json c#, deserialize json c#, deserialize json in c#, deserialize json to object c#, deserializeobject c#, json converter c#, json to c#, json to c# class, json to c# converter, json to csharp, json to datatable c#, json to model c#, json2csharp, jsonserialize c#, list to json c#, newtonsoft, newtonsoft json, newtonsoft json deserialize c#, object to json c#
Id: hLYHE1kIOpo
Channel Id: undefined
Length: 23min 16sec (1396 seconds)
Published: Sun Mar 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.