Nested JSON & Maps | Build Models | Nested Models | Parsing Json | Flutter Dart

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is a basic example of nested json over here   of course just a diagram and i'll walk you through  very quickly now here in general nested jsons uh   or any kind of json first it will have curly  braces and inside curly braces it will have a key   and value this kind of section or part like here  i have these two curly braces and so that makes   it json and now inside json we have a key and we  have a value okay and here i have another key but   inside this key well i have uh over here another  json you can understand it like this section   okay so that's another json so json inside json  that's called nested json that also means that   outside you have curly braces and inside you  have another curly braces and within the inner   curly braces you have more key pair values so  we know that first thing first condition for   uh format to be json it has to have a key and  value so we have over here so that's the first   json and inside it we also have another key and  values wrapped around the curly braces so that   makes it the other json or the nested json okay  so in general nested json would look like this   outside curly braces and internally it will also  have curly braces okay so that's the most basic   idea all right so that's all our theory now we'll  come over here and over here you can just type   it in dartpad.dab and it will take you this kind  of page and over here we'll clear some ideas now   over here definitely we have seen what is called  nest adjacent but before we go further because   we have to clarify another idea which is called  map and later on we'll see how map and jsons are   connected and we'll see how map jsons and models  are connected okay so they are all connected   so over here we'll clarify part of the idea  about map so i'll go ahead and declare a map   variable and i'll call it bar and i'll  call it my map okay all right now in dart   we declare a variable and variables body section  it if it has curly braces that makes it a map okay   now just like json we saw over here over  here key value map also has a key value   for it okay so that's what we're going to do  here we'll do the basic one first i'll do name   and i would say does to view that's my  name though and now here i would write   age here i would write 34 and  here i would do city do shanghai   okay all right so that's what made it a basic map  okay so anything wrapped around curly braces and   inside that you have this key and value just like  jason that makes it a map of course in dart there   are other different ways to declare map now for  those understanding you have to come over here   this is another article that i created uh there  are a few different ways that you can create   map like this is the first way just now we did  it's the second way where you can have first   create a map instance using this constructor and  you create the case and put the value directly   and there's the other third way okay now over here  you mentioned the type of the key and the value   so in our case here we are saying  our keys they all should be string   and the values are dynamic so that means they  could be string or they would be uh integer or   any other type okay so that that's what makes  it dynamic i will come over here and basically   in this section would be sticking to this  format of map but here actually just what we saw   and over here the basic idea is same because over  here we have this is these things are called keys   and they are all string now the value it's string  or integer as you can see over here all right   so over here we see this is how we declare a  map and create values for them in a pair within   using keys okay now we'll see how to access them  okay now over here and generally you'd go and   you do like my map and if you over here if you  write your keys name like name so name is my key   over here for the first one and i'll just hit run  and we'll see it prints values okay all right so   that makes sense so that's how you access  the items inside a map using the keys okay   well there are other different ways  but here we'll just stick to this one   okay all right so that's the most basic map  okay but just now we saw a map looks more like   jason what do we saw over here okay  yeah within curly braces key and   key and value key and value in this format right  so you could say they are relatives or cousins   all right now we'll see what is a  nested uh map okay or nested map or   the same over here what is nested json okay now  while over here i already have this one okay   now i'll create another field and  over here i would i would call   say address okay now after that i'll put it  like this i'll put this brackets over here   okay now for address i'll create one map inside  of it all right now here i'll say country   and here i would do say china okay and here  do city and here i'll write say shanghai   all right now here i'll copy this  one and put a comma over here   and i'll put it one more time again now here  i'll write say bangladesh and the cd daca   so here over here this is a map right but while  this section has a key pair value right okay but   what is this now here in dart whenever you see  something is wrapped around this kind of braces   that's called a list so what's what do we have  here so here we have list of maps so this is   a list of maps we have two maps you can have  more maps okay that's what we saw earlier if   we go back to our json file remember here's json  format but the most or the most amazing part about   this json and map they're pretty similar okay  yeah i would say a lot of times 99 similar okay   yeah so when when if you understand what is json  then get your header on map becomes pretty easy   okay so now here i have this key and while in this  case this is also key but this key contains a list   and the list contains a map okay so here i have  list of maps okay now for this one pretty much   how how do you want to access this one  but here just now we saw we could do   my map and here i can print name all right now  i can run it and i'll see this one my name over   here print it but of course uh if you try to do  this on my map and let's see let's see the result okay so it does print whatever inside  of this okay yeah now there is another   way to access this thing not like this say i  just want to have a particular value from it   like the country for the first map okay i  want to just get the country or the city name   whatever you want i think i would type  over here though all right anyway so   you can we can do that but how how we do that  now here i'll type over here now i know that   i can access this one right so here what i'll  do i'll create a new variable and i'll call it   address list and here we do we'll save  this list which is coming from our map   will save that in a new variable and i will  call it address map okay all right so let's see oh that is my map not maymap all right okay so now  i took this list now this list definitely coming   from here and that's the maps key so i took the  maps key one of the keys and interestingly this   key contains a list right so now this is a list so  if this is a list i can access it okay access the   inner variables or inner items okay not variables  the inner items and how i would do that well here   i would do address list as list we need to cast it  to list and then we would use 4h okay and it takes   elements and each of the elements we'll call  it v or we can call it e this is the convention   okay now here we can do a print so here we do e country now we'll print it over here  let's see and let's see how things go   all right so the first country is china  the second country is bangladesh okay   so while when you convert it to a list  and each time it will take one of them   and after taking that it will try  to take the key whatever the key you   mentioned over here and from that one it would  print the values so over here we could do city and   we'll see different results right so  now we know what is nested map and so which is pretty similar to our nested json  well now once again i'm going back to this   one again and again because in general  you in in your app in your flutter app   you'll have your json this kind of json actually  coming from the server and once you take it from   the server it pretty much look like this okay  so actually once you take it from the server   i would call it map instead of json even though  the convention they say it's all json but   the format it gives you most of the server  side when you return data for restful api   the data is pretty much json response and  most of them they have this kind of format   and we'll see that uh the restful api we are going  to build and use that also returns this kind of   json and which is pretty similar to map and if  it is similar to map we can do this a lot of   things because it becomes a map all right okay so  that's the basic idea about map and nested map and   nested json well now how to go ahead and build a  model for this one that's what we'll see now okay   now we'll go ahead and build a model now why you  should build a model well once you build a model   there are tons of benefits okay well you really  don't want to work on your flatter project or   app using map and access them like this like  earlier we did okay so for example my map   name like this okay you don't want to do that  because as your project gets bigger it would be   hard to maintain especially the naming thing if  you want to change that's very difficult because   you have to name in a lot of places but once you  do build a model you just change in one place and   that makes your life easy okay and there are lots  of other benefits and this is also a good practice   so i strongly suggest whenever you have json file  or map in your flutter app and if you're going to   do some build a class or make some operation on  them so try to build a model based on your map and   it will make your life a lot easier and the model  building model class idea is same throughout other   framework or languages okay so once you master how  to build a model based on a map you'll see when   you try to learn another language it's becomes  pretty easy okay the idea is always same just   like we saw how json and map they're similar  right so the ideas are all transferable okay   anyway so to build a model first we'll create a  class over here and the class name i'll declare   person now why i'm doing that because here  in this map i have person's information okay   that's why but you can name it anything you want  here first i'll go ahead and type string name   into h and string city well for now i'm not  going to put this one we'll do it later for   now i'm going to cut it take it out well now here  you see this is a small class where i have this   members and the members names are same as  the keys in my map right so in general when   you build a model and you should have fields and  those fields they should relate to your json file   or map keys names okay so these are the keys  names that's why you should do that now this   is a small map and for this small map we build  a small class which is a model right of course   we still have constructor so we'd go ahead  and do constructor over here we'll do person   this dot name and this dot h and this dot city  okay all right so now it makes uh now it's a   pretty much complete class over here based on  this map so we also say that this is a model okay   now for such a simple map we have a simple  model okay so all the fields from here are here   now take a look we have this one so what  does it tell us now it tells us that this is another map right while they're repeating  but the format is same so inside this value inside   this key inside this key there is another map okay  so that means here we have to go ahead and declare   a map not string or end we have to more like  something that's uh based on this map actually   not really declare a map based on this map now  earlier we saw that we get this for this right   now that also means that for this section we  build a model okay so here inside for this section   we have to build another model now we'll have  two models so that tells us that it's a list   of models over here right so that means  for this section i have to create a model   and for the related field inside  this model i have to create a list   in that list i'll put this models okay because  it's a list of maps from each map we make a model   so here from list of maps we create list of  models okay so the ideas are pretty similar okay   all right now over here what i'll do  i'll go ahead and copy this one first and   i'll put it here and i'll call it address  okay yeah now here just think this address as   a separate map now it is two field country  and city okay so here i would do country   and well we already have city so we'll just put  it there okay now here we all we need to do need   to remove this one and here we need to write  country okay remember they should they should   be similar all right now here instead of person  we would write address okay okay so now i have   a large map and for that map we have a model but  of course this model is not complete inside that   map we have another map and for that map we build  another model now because this is a big map it   has another map so inside this model will create a  list of models okay that's what we're going to do   so that's why here we'll write list address and  here we call address okay hopefully it makes sense   now once you do like this over here you  also have to make some changes here you do   this address and yeah we are good to go that's  it so that's how you make models from nested map   or from nested json you make a model where they're  nested models so this is a model instead of this i   have another model and that is nested model  okay all right now before we go ahead and   finish this section we have to build another we  have to learn another important concept a lot of   time it'll inside you in this class you'll have  another method that's called from json method   this is the convention but i think you can call  it anything you like okay now it takes parameters   so the parameter type is map and we know that map  is pretty much more like string and dynamic type so most of the time when you  go ahead and build a model   and inside this model model means class right  so inside this class you will have a method   and that method is called json okay so what  actually you do based on this method actually   you create object okay once you create object  if you know say i create an object from person   so object name is person now i'll be able  to use a person to access the fields it has   like for example age or name like that of course  here we can't there are other steps to do anyway so so this method takes a parameter and that  parameter is a map okay and the map is string   and dynamic earlier we learned that map could  be string the key could be string and the value   could be dynamic okay that's what we learned  early and this is what we are doing now okay anyway so here we are so what it means that in  my somewhere in my app i'll call this function   or call this method and then i have to give it to  a map so i have to give it to a map like this okay   all right and once i give the map to it the values  would be stored in json over here so i can use   this value this variable to retrieve those values  and initialize them over here in this field okay   and based on that we can create an object okay  hopefully it makes sense so that's why he would   come over here and here we do uh jason now jason  should have name as a field inside of it and then   here you do h and jason once again h remember what  we're trying so this one will have access to the   map you give to your from json uh method okay so  as i said earlier it will send something like this   to it over here so that means this fills you have  to use over here to create assign the value to   this fills and create object so here we are now  and here we have this city and we do jason city now while next part is quite  interesting now here you see we have   a list right and this list takes a model so how  to work with this one but we know that this is   a list okay now here it says it could be null so  we can go ahead and do a check if it's now or not   if it's no will not do anything if it's not null  what we'll do we'll go ahead and work on this   so if it's not null like this okay so  here i do address now here we do address   and uh we'll write this statement that means  create a list okay create a list and that is   uh empty address okay well so here we are creating  a list but because you see over here that means   empty and we assign it to this one of course  this is this one okay but at the same time   this is means empty list right and the type  is address okay because the list has a type   it could have type we know that okay so that's  why we are initializing this empty list to this   one okay hopefully it makes sense now uh this is  a list over here so this is a list okay all right   now we know that our address this section is  also a list because remember this one points   to this section and at the beginning of this uh  section earlier we we learned that this is a list   list of maps okay so this is a list now here  what we could do we could use this one i'm sorry here we could use this one okay and then  each list has a method which is called forage   okay so that's what we're going to do so  here we do forage or maybe it might have an   error that's a null null safety thing so what  we could do we could come over here and we   do this one as list okay just like earlier we  saw and then we can access for each method okay   now if we do that you can  access them one by one using any variable you want and then we'll put the  curly braces and then we are here okay so what's   happening here so this one even though it's a  list but we force it to be a pure list or the list   actually if dirt likes okay so we are casting it  making sure because after all this is coming from   map or json file okay it was not purely list so i  cast it to a purely list okay now after that this   e because it's a loop this loop would run over  here two times so first e would refer to this one   second e would refer to this one okay now  remember this is a map we learned that for map   we can create a model and we already did  this one okay so right now as i said earlier   we'll pass this value and create an object  okay now of course to be able to use this one   over here uh we we we need to create another  method over here and the method is once again   pretty much like this one okay all right so  what i'll do i'll go ahead and copy this one   and i'll put it over here i'll just simply  change the name person to address now over   here we don't need this section because we don't  have for this section we don't have any nested map   or json okay now i'll remove this and we're good  to go and over here we need to write country so what do we have here so over here we can just  simply pass a map and based on that map we can   create an object right so this is a method  that also means that we can call this method   from somewhere else from some other classes  okay so that's what we're going to do now   do remember this section actually pretty much  same as this one only difference is this has a   nested list which it doesn't have this has a  nested list because this has a nested map right   anyway so now we'll come over here now what you  could do we'll call this method okay so here we do address add remember this is the list empty  list so we can put things inside it and we   are going to create objects and put that  object inside this list okay so here we do address dot from json okay now all we  need to do we need to pass this one okay   so pass e all right and that's all let's see we have an error so here you can say that it's not null yeah  i'll talk about the error a little later   now let's take a look at this one so over here i'm  looking through this address this list over here   and each time i look through i have access to  this one this is a map now i'm passing this map to   this method and it process  the information and it gets me gets me an object and i put that object in this  list okay and object type is list because we   said that sorry the object type is address  okay so we are putting them all in a list   okay so hopefully it makes  sense we'll go back and see   how to create uh objects based on these models  okay well so here first i'll go ahead and   create a new variable i'll call it var and i'll  call it obj and here i would call person dot from   jason okay remember earlier we saw  that we should pass a map to json okay and we already have a json over here and what is  your json so our json is my map okay or map all   right yeah that's all it takes so now we just  created an object using our class our model   and we supplied it to a map or a json like  this okay now interestingly i could do a lot   of interesting stuff with that i could do vga dot  name okay and it will print the name for me over   here it did and i could do h all right now it  will print the h for me i could also do address   okay and of course uh here it will print this  one because over here we are creating object   and calling this method so in this list  this is the list which i'm calling over here   which relates to this one so now it stores  because of this one inside this list   over here it stores instance once it's create  objects okay of course we can access that   now for for that one to be accessed here  we need to do obj the map okay map map   uh well sorry no really like that say let's  create a new variable first and we'll call it   my address and here we do obj dot address  so i'm just taking this chunk over here now   i want to access each of the element within it  and for that one i would do my address dot map let's see okay now here i can  create an instant i can access   each of them so this e would refer to each of them  as a null checker make sure that it's not null   okay all right now here i could do print e  dot as you see this fields are already there   because it's an object it's not a json  anymore okay so once you have object and   you use the dot operator you see the fields  immediately okay hopefully it makes sense all right let's see okay now we need to convert it  to list because map is not a list so now we'll see over here let's see now e country  are we making any mistakes over here   oh sorry i i tagged it nate wrong over here i  need to take it country so it couldn't find it   anyway so now we'll come over here and  we'll see the country's names are there okay   also that's how you build a class or  a model based on your map or json okay   and that's how you create nested models  or objects based on your nested map
Info
Channel: dbestech
Views: 19,929
Rating: undefined out of 5
Keywords: flutter nested json parsing, flutter nested maps, flutter nested models, json, dart nested json, dart nested models, flutter build models
Id: yrsvM40NzjM
Channel Id: undefined
Length: 31min 20sec (1880 seconds)
Published: Thu Jan 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.