Deserialize JSON Data to Custom C# Class Objects using Newtonsoft.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey team welcome back to my channel in this video we are gonna do some c-sharp modeling of some simple to complex json objects [Music] let's begin file new project we're going to do a windows console app bam bam bam like it and then we're going to move this thing up so i can click this button next then i'm not going to call it console lab i'm going to call it model that json [Music] i'm going to hit create and now because you know i'm doing json you know i have to load json in my project nougat master okay browse there there's a newton's off right there all righty click on this little jc install then uh say okay okay all right so now our project has been set up to do that [Music] okay this is example one data is nice and simple we have two fields a c id and that data type is integer entity name it is a string now what do you need to know here well you have to be able to look at the data type and know what it is you have to know hey is this an array or just a scalar variable is this a string or a double what kind of data type is this so you need to know that so when you look at this file these are the field names these are the values so filled value that's the way that works now just because you can type in some json that doesn't mean it's valid so before you start messing around with a bunch of programming let's notice what i did i went out there i took that same statement to a validator and it said this is valid jason now the first thing you do is let's do that first variable public int cid get set our second variable was called entity name and it was a string so public string entity name get set now these spellings have to be identical you can't come over here and put a little end there and expect it to work now you know that now don't make that error that's a painful area and it could take a long time to debug that so make sure you're smarter than that let's look at the next thing so we've created our model it's called my model i use cid control c make sure i spelled it right entity name make sure i spelled it right then we're ready to go well let's make a variable for that file name string file name equals what is my file name eagle well i know i don't want a bunch of slashes so i'm gonna say verbatim so verbatim quote quote single so now i have to go figure out where that file is so notice here i have that that file so right click on that you see where it says copy full path okay it's now on my clipboard and now i'm just going to come here and paste that in there make sure i have my quota at the end so start and it all looks good right now what do we have to do we now have to i just included all my newtons you saw when we started it up i did a nougat made sure newtonsoft is available so now i'm going to say string json text equals file dot read all text and what am i going to read file name now i was able to do this because i included system i o now remember this json that's on a file i know that looks like something you want to call json but it's really just a string that's a ray of characters that's all it is this is special because we have put a lot of programming into this resource but it is just a string so don't get too hung up on you know like what it is so and what it is to me is it's just a text file i'm about to read it but it's kind of a special text file now we're going to use that newton soft and we're going to deserialize that json object into our c sharp object so how do you do that you say data how about we say var var data equals newtonsoft dot json dot json convert dot d serialize now are we in the deserialize deserialized object yes that's exactly what we're going to do and what was the name of our object well the name of our object was called my model so copy that down let's put it in our program and then guess what we're going to do here open close come on open close and then see the name of this file ctrl c ctrl v and we're done with our program let's run this and see how this works ready okay team let's run this little jesse i've done everything that could let's make sure it works ready i'm gonna f10 into this look at my file name my file name went out there and it got me that full full path notice i used the app symbol before that so i don't have to put all them extra backslashes it's called verbatum now what i'm going to do is i'm going to take that file name and i'm going to read it and the contents of that is going to go into that json text let's do that f10 okay done now look at jason text it's got all that jason with all those backslash new lines and carriage returns hey this is great i love this then what it's going to do it's going to take all these words and it's going to map them to the object that you just created my model that's what it's going to do f10 now let's bring in a debugger and let's verify what i just said okay so we know we had a cid a 750 as an integer we know we had entity name my company is a string we knew that now if i look at my model notice c i d c i d int entity name entity name string right that's the value and let's just go take a look at that uh that json okay so cid 750 entity name a company name so as you see guys this first one very very simple but hopefully it build a skill for you [Music] in our second example the json is just going to get more complicated so each example gets more complicated so now you can imagine this right here i come down and i say entity stock shares and then i say uh you know what that is its own object and then here i'm going to give it some kind of label so i know what that means in the future remember around all these things we have to use the quotation marks and we're going to say here this is common stock common stock of that is outstanding and we're gonna put a little description on that and we're gonna say uh this is the uh number of shares does that make sense so we're making a little bit more complicated and these are common shares and it's the number of shares that this person owns now notice here this is a another object of this guy so we just have to do some modeling that makes all that make sense we validated the object and it came back valid so we just added a little bit more to our json we went to a validator and it validated to be true and now let's think for a moment cid entity name cid value cid value entity stock shares is it like this line three and line two no it's an object so we can't continue this same model that we did here we gotta be a little bit more creative now how are we gonna do that well think about if this was my object and let's go ahead and create that object from scratch so how would we do that we would come down and we would say public class and then let's call that entity stock shares right that is the name of my object and it looked like we had two fields to it and we're gonna say public label and then we're going to say uh well we're going to say more than that it'll be a rookie string and then get and set right and then public string esc get said now a lot of people want to have typer casing here but we're trying to try we're trying to solve a problem and in our json we have to call it what it is if it's little case uppercases whatever it is that is the variable name we just have to deal with that but notice here my entity name i'm i want to use this name so what i did is i i begun this with a underscore e and then what i'm going to do is i'm going to come back up to our base and then i'm going to say public what's my data type um data types and object entity and then that again but i'm gonna take that off call that e because that is the way it was spelled right it was spelled that way see how it's little e here well little he here little e here little e here i can't you know change the spelling just because you know i want to you know somebody should have thought about that when we modeled this this json but you know this is what we have so we're modeling what we have so hopefully you can see that you're starting to understand that now this is an object it's not an array it's an object and let's go ahead and code this up we have to continue here with get and set and we are done modeling now you're probably thinking what changes do we have to do here are you ready for this none we are going to deserialize that json this through this into this that's it all right let's do it ready f10 f10 gets the ball rolling compiled a blue star i got my file got json let's see if we have all that extra stuff in there you see all that extra stuff that we have done down there f10 do i have my entity stock shares yeah i got it right there now guess what i'm about to execute line 24 f10 i'm gonna bring over a debugger data now look at our data cid entity name oh there's our new object our object has got descend description and label that's the way it works every time one two three let's do something more complicated now [Music] in our third example i wanted to introduce some arrays to make it just a little bit more complicated and how did i do that well notice that i added one more key to entity stock shares i called it units and then i came down here and i said trades but i said oh let's let's make that an array and inside the array i have objects so that would be called an array of objects but that is under units so what we have to do like every other time is we have to go out and validate that to see if this is even you know good json let's see what happens so i went out to a json validator and notice that this website says this is valid json as i assumed now here's where the magic begins we now have to model all this new json it's just not gonna show up in our object data i mean we've got to do some modeling that's what this video is all about learning how to model let's see how i did that step one what did we do new here well we added this thing called trade trade is an object but it's an array of objects let's go and build up this trade let's go and build up this trade object so i'm going to come down here and i'm going to say uh public class underscore trade you know why i do that and then uh so what kind of fields do we have in here well we have a account date of trade ticker trend type quantity and cost per share sounds pretty easy now what else do i have to know about this well i should know that this looks like an integer a date a string string integer and some decimal okay that's what i'm going to do so what what do we say here we say public and acct get set and so i don't have to type all this all over and the second one was date of trade the third one was ticker the fourth one was tran type the fifth one was quantity and the sixth one was cost per share and we got an an int a uh that looks like a daytime daytime the next one is a string string integer and a decimal so that's my object for that uh trades now if i come back over and look at that what are we dealing with here so account date of trade ticker trend type quantity and cost per share okay cool now what i want to do is it is possible that i've misspelled something you know because that's the way it happens in real life so i'm just going to copy these in here and i'm going to do them all because it's always the one i don't do is the one i misspelled trend type and then that quantity and the last one was cost per share okay all right cool so now that we have that all done what do we do next well let's take a look at that jason again see that sits inside unit well if we kind of are lost here and we can remember what we did for end of these stock shares isn't this word labeled just like trades but you know this is an array of objects but this was just a label in a string but how did i do that let's go take a little look-see well see how i took this up there and i called it that so can i do the same thing for this guy can i bring it up so let's see how i would do this right here ready so underneath this guy i'm going to make another class and public class units and then units has what units has public trades has an array of trades trades get set right so what does this mean so this is that class object and this is an array of them so i have an array of these how do i know that well this bracket here open bracket close bracket this is an array these are objects that sit inside this array right so i came out here and i said okay here's my trade that that object let's make an array of them and let's call it trades well that wasn't too hard so what do we do next are we done no we've got to add it to this guy right we've got to add units here so how do we do that very very simple we just come up here and we say public underscore units units get set so notice the way our it comes down in it says this it says sid entity entity stock unit sid entity entity stock oh see an entity stock share we're in that we're underneath this now this is units is a child of entity stock shares that's why we put them here okay well talk is cheap let's see if i get this thing to work ready f10 f10 and then bam bam bam show me the debugger uh debug quick watch all right and that is called data there you go add watch i've already got them in there let's delete that okay so let's do this bottom one so i got 750 a company and then entity stock has a description and a label and it also has a units and that units has trades and each of those trades have objects and there you have that okay so let's make it a little harder okay [Music] we're in our fourth and final jason to c-sharp modeling this is a great learner here uh one thing to point out programs.cs notice i have not changed this this has not been a lesson about newton deserialized object this has been about modeling and when we take json we just need to model our c sharp objects to reflect how the data flows now once again my steps do not change so i took all this json to a validator 131 rows and it came back as valid so all of the data is good so we have valid json so that means i can write some c sharp programming and notice that we've changed it quite a bit i now have accounts accounts have different parts of the world right let me see we got a ecuador and mexico so it's very very complex um you know like a you know this is pretty comprehensive in fact if you have some other json that needs some attention just learn this one and then just baby step through your problem and i'm pretty sure you'll get to get it so hopefully that's what i've taught you in this lesson about taking baby steps and solving problems there's the json let's look to see how i modeled this notice i started off at the top cid and entity name and then i have this thing called accounts let's take a look at that so cid entity and then accounts and then notice accounts umbrella goes to the top to the bottom and then in each of the sections you know each country we have you know a a state so what i did is i came down here and i created another class called world investor and then i gave it to usa slovakia ecuador and mexico so this one class object here is now pointing at the four countries now notice that entity stock shares points at this class and this one points at unit and unit points that trade so all together look at that it's just 40 50 lines of code of class objects that solve this problem now this is a pretty comprehensive json object but it's not hard it's comprehensive but you know you should better get this so take your time and look at this code you know the way it just drills down and just take your time when you're developing a model okay i want you to know that uh of all of the source code here the only one that i'm going to put out on the drive so you can download it is example number four this is the most comprehensive so you will get models.cs uh program dot cs and the json so you're gonna get all four of these so you don't have to retype this stuff okay all right so let's uh start here with our program let's uh do a break point on line 24 and let's see what happens so it's compiling it just built it and now i'm going to do an f10 and then let's look at this data do we have a debugger debug quick watch okay and the thing is called a data ad and notice here so it goes account then accounts have ecuador mexico slovakia and usa and then notice usa has all the way down to their trades okay that's usa slovakia has all the way down to their trades and let's see mexico mexico has all the way down to its one trade and then ecuador is null what does this mean well you know exactly what that means it is not configured correctly let's go find the error okay so i'm gonna stop this mess right here uh do you want to stop close stop now what we're gonna do here is go into our model and let's look to see what we do with this ecuador ecuador and it's a world investor and then i have to go look at the json ecuador did ecuador get spelled correctly oh there it was i had transposed the u and the a let's run this again you see something as simple as not following steps will put you in the headlock ready f10 now let's pull over our debug window and notice data accounts oh look at ecuador oh ecuador's got data now oh so everything does work if you do it right look at that they got a two uh binko i can't even pronounce that word and i'm okay i'm gonna try so notice we were able to do it now i didn't follow my steps about making sure that i typed everything correctly and you saw what the kind of error it makes so take your time when you're modeling because modeling is everything okay team that's all i have hope you learned something [Music] you
Info
Channel: Software Nuggets
Views: 1,752
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 tutorial, 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#
Id: udEjGSXncEg
Channel Id: undefined
Length: 26min 36sec (1596 seconds)
Published: Wed Sep 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.