Deserialize JSON using C# and Newtonsoft.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome back team in this video we're going to learn how to deserialize a json object and store it into a c sharp variable just a moment ago i created this file customer.json let's take a look at this file as you can see we have several records that make up this json file and right now there are seven fields that comprise each record you can see that the customer id the customer name strings and then we have number of employees as an integer and pays on time is a bullying so we're going to write a program to dereference that so right click on references manage nugets and we're going to be searching for newtonsoft json install it once it's installed we can write programs that use this library pretty cool right press pause now to make sure you get these using statements so right click on the project add a class and let's name this class customer dot cs let's add the public scope and the serialized attribute to this class and now we just need to look at all the fields and apply them to our class object you can add one field or all the fields to the c class objects and the data types must be agreeable as you can see in the side by side customer id equals customer id customer name equals customer name the cell the spelling must be identical as you can see here i declared number of employees as an integer which is nullable let's look at the json file notice number of employees in the first wreck answers 10 20 in the second one but it's missing in the third but i still want to be able to process this fill so we declared number of employees as a nullable data entry point in the next one date last order is also using an optional you can see here it's used in the first record the second record and the third it is missing it is a nullable data type and you can see the last field pace on time is a nullable data type of type bool it is in the first record the second record and is missing in the third it is a nullable data type we need to process that so the goal here is to take each field from the json file and add it to your class object remember the spelling has to be the same but you can only pick and choose the fields that you want and make sure your data types are appropriate nullable if necessary make sure your c-sharp closed has the newton's off using statements and we're going to be using some file i o so make sure you use system dot io let's add the method example1 simple class object here you can see that i have assigned fn file name to the string c slash youtube when you just look at that it doesn't look correct because when i copy it out of file explorer notice that there's only one slash between the folder names unfortunately when we assign it to a string name the slash is actually an escape character and it has a lot of meanings so what we have to do is if we want a slash in our screen we need to add another slash to say hey he wants a slash now there's another way to do that and it's using the verbatim identifier that's the add symbol and notice here i just applied my string after that and now i can apply that to string name just the code looks cleaner when using all them slashes now let's test to make sure that file exists before we start doing a bunch of code notice file dot exists that came from that using above and file name came from line 27 press pause here because this is the gold here you can see that i'm going to take that file name i'm going to read all that text and then that deserialize object is going to take that json code and sign it to my c class object it's going to look for that spelling customer id and then it's going to assign it to my class object in c sharp so for each of these field names it's going to go one by one and assign it to the object that's all it does that is what's considered serialization and deserialization after the file has been read deserialized and assigned to customers we need to return that record but it is possible that we didn't find that file there was an error the path was wrong imagine if i put 3 on customer when it got on line 30 file exist it would be false so would not do that deserialization so we want to return null as you can see on line 20 i'm calling the method on line now 24 and i'm going to get the results of that function now the thing is i have two options i can either return a class object or i can return null let's program it appropriately if customers not equal null that means i got data i can then loop over that and show the output in the output window that's through system diagnostics which is pretty cool you can click that button to clear the output and then we'll be able to run the program and just show the output as we need let's rebuild the solution and see if we have blue star and we do everything is working fine okay let's press the f10 button to step through this program f11 to step into the method and we're going to see if that file exists if it exists we're going to go in there read the contents of that file and then deserialize that and assign it to the customer variable then we can return that object to the calling to main but as you can see here we know how to parse records now we knew that our data structure customer was created correctly we put data types as nullable when they were missing throughout our class object our json file now you can see that customers is not null it has three records that's how many records were in the json file and now we can just loop over that notice the output going to your output window and that's how you read a file deserialize it and consume it and this is the takeaway this is all you really need to know about this video just pass in the file name give me a good object deserialize it and return it and that's all i have if you have any questions or comments please leave them below your input is very very valuable if you found this video informative please click the thumbs up and lastly if you'd like to show your support please consider subscribing see you on the next video
Info
Channel: Software Nuggets
Views: 4,322
Rating: undefined out of 5
Keywords: JSON, Deserialize, Newtonsoft, Assign to List Object, Read JSON file, Convert JSON object to c# object, deserialize json c#, newtonsoft json, json deserialization and serialization code in c#, json serialization, c# json serialization, deserialize json, c# write to json file, c# read json file, read json file in c# using newtonsoft, json to c#, string to json c#, json c# deserialize, read json file in c#, newtonsoft c#
Id: cGKA8wRCA0A
Channel Id: undefined
Length: 8min 26sec (506 seconds)
Published: Sun Apr 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.