Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
In the last section, we added in an image model class with this class right here, we're giving darte
a better idea of what values and what type of values we expect to extract from a piece of JSON data.
Now, the image model is definitely working the way we expect, but you'll notice that on this sign
right here where we create our instance of an image model, we have to write a little bit of laborious
code, like we have to look at the past JSON hash or that powers JSON map and we have to pull out both
that ID property and the URL property.
Now, if we just have these two properties inside of here, well, it's not the worst thing in the world.
But you can imagine that if we had like 10 different properties coming out of a blob of JSON, we would
definitely not want to have to write them all out in a real long argument list to the image model constructor.
So in this section, we're going to figure out a better way of creating a new image model instance out
of a blob of JSON.
So ultimately, what I want to do is only write something that looks like this.
I zoom in here, just you can see that line, so it would be really great if we could create a new image
model and just pass in all that, Jason, and have the image model itself, figure out how to extract
properties from that and initialize itself.
But here's the issue with this.
If we mutate the image model constructor and say that this thing always expects to be called with some
JSON, then we lose the ability to more manually create an image model.
Like, let's say that you and I want to create an image model manually and you and I want to pass an
idea of ten.
And, you know, whatever you are, we might want to pass in.
If we change our image model constructor to always expect to get some JSON, we'd lose out on the ability
to make a image model with our own custom arguments like so we would instead have to assemble a map
object and pass that in.
So it seems to me like it would be really great if we had more than one way of creating an image model.
Fortunately, we do.
So that's exactly what we're going to do here.
Are you going to clean up that line of code?
There we go.
So the idea here is that we want to have the ability to create an image model out of a big chunk of
JSON.
But we also want to have the ability to create an image model out of like a list of arguments that you
and I manually provide.
So to do so, we're going to go down to our image model class.
Right now, we have exactly one constructor inside of here.
The constructor is designated because we use the name of the class for this function right here.
However, we are allowed to add in multiple constructors to a single class if we want to.
So underneath that existing constructor, I can add in something called a named constructor, we can
add in a named constructor by writing out image model, a dot and then the name of the constructor that
we want to create.
So in this case, we are trying to create a image model out of some JSON.
So I'm going to give this constructor a name of from JSON like so.
So now we can create an image model either by calling image model and passing on the ID in the URL,
but we can also create an image model by calling it a new image model from JSON.
And then we'll pass in our JSON data to this thing and have the image model figure out how to extract
properties out of that and assign them to the ID and URL instance variables.
So to make sure that this thing properly extracts some data, we're going to receive the argument.
Of Jason, and actually we can't call this we don't really want to call it's Jason right here, because
technically up here, this package converts, imports an object called Jason already.
So I don't really want to reduce that Jason name anywhere in my code.
So I'll call this right here.
Overuse that variable name of like, how about past Jason or something like that?
It's not a big deal.
If we call this thing Jason, there would be no conflict.
But we're just gonna make our code a little bit more clear and just call it past.
Jason, just make sure it's evident that, yeah, this is supposed to be some past bit of Jason.
So then after this, we can add in some curly braces that are going to give us the ability to extract
some values out of that pass JSON object and insert them into our instance variables of ID and URL.
All right, so inside of here, I'll say it is past Jason, I'll put my square braces down or square
brackets and I'll do it and I'll do the same thing with you earlier.
Past Jason Yooralla like so.
OK, so that's it.
Now we have the ability to create an image model with the default constructor right here where we're
going to call new image model and directly pass in in Edina, YORO.
But we also have the ability to create a new image model from some JSON where we just pass in this big
blob of JSON data.
So now back up here, we can make use of that new constructor, so rather than saying new image model
and passing in all that JSON, we're going to say new image model DOT from JSON like so.
So now the result is the same as it was before we passing this JSON data, but we don't have to like
iterate all through all those different properties.
We only had to do that one time when we set up this named constructor down here.
After we call from Jason, we get back our new instance of image model, we assign it to the image model
variable and then we can freely access the different properties on it.
So now I will run this again.
And we should still see our oil up here, up here.
And come on, I know this thing wants to run is just being a little bit hesitant.
Let's try let's try a different property.
There we go.
OK, you are very good.
And we can do it as well.
And we'll get back once again.
OK, so that's it, that's how we handle JSON data.
We get back some rodgerson from maybe making an API request or whatever it might be.
We call it JSON code, and then we will very commonly make a model class.
We might add a constructor to it called a named constructor that will take that big blob of JSON.
It'll extract the values we care about in assign it to the new instance of the model.
It's now that we have a better idea of how to work with JSON will flip back over to a real project and
we're going to put this image model class to work.
So quick break and we'll see you in just a minute.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.