Ex3: Step-by-Step, I will show you the steps it takes to build C# class objects to DESERIALIZE JSON.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey team welcome back to my channel in this video we're gonna dumpster dive into some more adventureworks database this video is where they start getting hard so let's do this together are you ready if you have already used example one and two you'll notice in example three here we have a new object employee pay and the complicated part of this is other pay it is an array of strings we'll get through this together let's do it if you're wondering how i made this json layout i did it with serialization let's see how i did it it all started with designing a few class objects in this project the only thing that's new is online 40 and 41 i added other pay and the employee pay object look on line 49 i'm actually going to create the object with create department list look on line 86 how i was able to give an array of strings overtime holiday pay and vacation pay in example 3 our object is getting bigger bigger means more fields more fields means more data take your time step through it and you'll do it okay this finishes up the create object now let's just serialize it let us model this json from raw notice i want to just call my class adventure works list so we're going to be using the list notation remember in our first video i showed you that the department could be either an array or a list and they both um deserialize the same so let us begin so what i'm going to say first is a public string and you see version and we're going to get set that now the spelling has to be identical so no uppercase lowercase you put what the case is over here notice if you want to use some kind of notation that puts underscores in your characters you need to change your your json but this needs to map to this so our next item here is look at this we have an object see key value the value is an array it's an array so how are we going to do that well we learned in the first one i can also use a list so i'm going to do that so what do i say here public list and then what are we going to call that well i don't have any choice but to call this department so i'm going to come over here and put department here and then say get set and then notice i'm just going to say underscore department here now this is just the way i do it i could be right i could be wrong but it seems like this pattern works for me successfully all the time so the next thing we need to do is we need to define this this class object so what do we say here we say public class underscore department and then open close curly now when you see department over here what kind of fields are we going to associate this with well we're going to do a department id that looks like an integer so what do i say here i say uh public int get set and uh name is gonna be a string so what do i say public string get set and group name public string [Music] gets that and then a modified date now here's the question this right here looks like a string but when we deserialize that does it go in as a string or a date time let's try daytime i'm hoping that's what it will go to so now i have another object and the object is called employee so i can't continue on here i have to create another object and how would i do that well public class underscore employee and then we come back up to this guy and then what do we say here well it is a list right look here do we have an array notice it goes from here i wish this would line up better but uh you can see this one goes all the way down to this one right uh bam to bam bam to bam yes so that one goes all the way down to here and the reason we know that that's an array is because of this bracket so how can we do this in here well we learned in video one that can either be a list or an array let's uh stay in the list so what do we say public list underscore employee and what do we have to call it here well we cannot be creative we have to come over here and just take this as it is and that is the way it is i get said now what kind of fields are an employee well it's kind of easy it's the same thing that we did for department so what do we have we have login id let's say what so i don't have to do all that typing let's uh block copy this so the first one is login id right ctrl c that and then the next one is job title okay and oh what is this now employee pay key value but value is an object so what have we learned well i've got to go create another object and what is that object called that object is called employee pay so what do we say here public class underscore employee pay now once again this has look at that it's a date time some kind of float and an integer and then an array so how do we do that well let's uh come get this and we can go fast so what do we have uh four fields so the first one is a a daytime our second field is a float our third field is an integer and our fourth one oh this is another type but we can actually do this in line because this just looks like an array and all we have to do is come in here and say that looks like a string array so a string array okay so let's go name these things appropriately ctrl c those over control v raid and then uh frequency and then over to or other pay and okay so now job title so it came down here and i had to say public list actually that's not a list that's a single object right so notice here no we say key value key value but here key and that's an object and not an array of objects just an object so we just say underscore employee pay and what are we going to say here just employee pay gets it so looking at this i believe this is the structure that we want and now we can deserialize let's go look at that other code let's go ahead and run this code i'll meet you on line 25 29 i'm sorry let's add an ad watch on departments and let's step through this okay departments oh we see count we know what count is now it's a list and we have two items in our list and department id one employee count two we know that's a list and we got two items in there as well notice that we have employee pay looks like it's just an object other pay is a string array should have three one two three we have our variables it is a date time variable so everything worked out as we thought and this example three is all done [Music] you
Info
Channel: Software Nuggets
Views: 13
Rating: undefined out of 5
Keywords: newtonsoft json c#, newtonsoft json deserialize c#, newtonsoft json, newtonsoft c#, install newtonsoft.json, model c# from json, json tutorial for beginners, json to c#, deserialize json c#, json to c# class, json c# deserialize, deserialize json, json c#, convert json to c# object, deserialize nested json c#, nested json to c#, model nested json to c#, The JSON value could not be converted to, JSON arrays deserialize, deserialize json array c#, software nuggets
Id: Qp4f6ZJKciE
Channel Id: undefined
Length: 9min 58sec (598 seconds)
Published: Mon Dec 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.