Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,040 --> 00:00:06,290
In the last section, we added in an image model class with this class right here, we're giving darte
2
00:00:06,320 --> 00:00:12,560
a better idea of what values and what type of values we expect to extract from a piece of JSON data.
3
00:00:13,700 --> 00:00:17,720
Now, the image model is definitely working the way we expect, but you'll notice that on this sign
4
00:00:17,720 --> 00:00:22,640
right here where we create our instance of an image model, we have to write a little bit of laborious
5
00:00:22,640 --> 00:00:28,490
code, like we have to look at the past JSON hash or that powers JSON map and we have to pull out both
6
00:00:28,490 --> 00:00:30,960
that ID property and the URL property.
7
00:00:31,640 --> 00:00:35,780
Now, if we just have these two properties inside of here, well, it's not the worst thing in the world.
8
00:00:35,780 --> 00:00:40,760
But you can imagine that if we had like 10 different properties coming out of a blob of JSON, we would
9
00:00:40,760 --> 00:00:46,400
definitely not want to have to write them all out in a real long argument list to the image model constructor.
10
00:00:47,120 --> 00:00:52,190
So in this section, we're going to figure out a better way of creating a new image model instance out
11
00:00:52,190 --> 00:00:53,810
of a blob of JSON.
12
00:00:54,320 --> 00:00:59,810
So ultimately, what I want to do is only write something that looks like this.
13
00:01:01,270 --> 00:01:06,580
I zoom in here, just you can see that line, so it would be really great if we could create a new image
14
00:01:06,580 --> 00:01:13,330
model and just pass in all that, Jason, and have the image model itself, figure out how to extract
15
00:01:13,330 --> 00:01:15,790
properties from that and initialize itself.
16
00:01:16,400 --> 00:01:17,620
But here's the issue with this.
17
00:01:18,070 --> 00:01:23,740
If we mutate the image model constructor and say that this thing always expects to be called with some
18
00:01:23,740 --> 00:01:28,570
JSON, then we lose the ability to more manually create an image model.
19
00:01:29,080 --> 00:01:34,180
Like, let's say that you and I want to create an image model manually and you and I want to pass an
20
00:01:34,180 --> 00:01:35,140
idea of ten.
21
00:01:35,350 --> 00:01:38,710
And, you know, whatever you are, we might want to pass in.
22
00:01:40,010 --> 00:01:46,440
If we change our image model constructor to always expect to get some JSON, we'd lose out on the ability
23
00:01:46,680 --> 00:01:53,250
to make a image model with our own custom arguments like so we would instead have to assemble a map
24
00:01:53,250 --> 00:01:54,670
object and pass that in.
25
00:01:55,320 --> 00:02:01,480
So it seems to me like it would be really great if we had more than one way of creating an image model.
26
00:02:02,010 --> 00:02:03,240
Fortunately, we do.
27
00:02:03,270 --> 00:02:04,800
So that's exactly what we're going to do here.
28
00:02:05,220 --> 00:02:06,540
Are you going to clean up that line of code?
29
00:02:07,530 --> 00:02:08,020
There we go.
30
00:02:08,490 --> 00:02:13,440
So the idea here is that we want to have the ability to create an image model out of a big chunk of
31
00:02:13,440 --> 00:02:13,980
JSON.
32
00:02:14,190 --> 00:02:19,260
But we also want to have the ability to create an image model out of like a list of arguments that you
33
00:02:19,260 --> 00:02:20,520
and I manually provide.
34
00:02:21,180 --> 00:02:23,930
So to do so, we're going to go down to our image model class.
35
00:02:24,600 --> 00:02:27,780
Right now, we have exactly one constructor inside of here.
36
00:02:28,470 --> 00:02:33,490
The constructor is designated because we use the name of the class for this function right here.
37
00:02:34,230 --> 00:02:39,720
However, we are allowed to add in multiple constructors to a single class if we want to.
38
00:02:40,550 --> 00:02:47,090
So underneath that existing constructor, I can add in something called a named constructor, we can
39
00:02:47,090 --> 00:02:53,840
add in a named constructor by writing out image model, a dot and then the name of the constructor that
40
00:02:53,840 --> 00:02:54,710
we want to create.
41
00:02:55,190 --> 00:02:59,630
So in this case, we are trying to create a image model out of some JSON.
42
00:03:00,110 --> 00:03:05,150
So I'm going to give this constructor a name of from JSON like so.
43
00:03:06,680 --> 00:03:12,260
So now we can create an image model either by calling image model and passing on the ID in the URL,
44
00:03:12,440 --> 00:03:18,050
but we can also create an image model by calling it a new image model from JSON.
45
00:03:18,440 --> 00:03:23,480
And then we'll pass in our JSON data to this thing and have the image model figure out how to extract
46
00:03:23,480 --> 00:03:27,980
properties out of that and assign them to the ID and URL instance variables.
47
00:03:29,060 --> 00:03:33,770
So to make sure that this thing properly extracts some data, we're going to receive the argument.
48
00:03:35,390 --> 00:03:40,910
Of Jason, and actually we can't call this we don't really want to call it's Jason right here, because
49
00:03:40,910 --> 00:03:45,890
technically up here, this package converts, imports an object called Jason already.
50
00:03:45,890 --> 00:03:49,250
So I don't really want to reduce that Jason name anywhere in my code.
51
00:03:49,730 --> 00:03:51,200
So I'll call this right here.
52
00:03:51,350 --> 00:03:55,070
Overuse that variable name of like, how about past Jason or something like that?
53
00:03:55,790 --> 00:03:56,900
It's not a big deal.
54
00:03:56,900 --> 00:03:59,300
If we call this thing Jason, there would be no conflict.
55
00:03:59,480 --> 00:04:02,680
But we're just gonna make our code a little bit more clear and just call it past.
56
00:04:02,690 --> 00:04:07,130
Jason, just make sure it's evident that, yeah, this is supposed to be some past bit of Jason.
57
00:04:08,210 --> 00:04:14,270
So then after this, we can add in some curly braces that are going to give us the ability to extract
58
00:04:14,270 --> 00:04:20,720
some values out of that pass JSON object and insert them into our instance variables of ID and URL.
59
00:04:21,910 --> 00:04:28,780
All right, so inside of here, I'll say it is past Jason, I'll put my square braces down or square
60
00:04:28,780 --> 00:04:33,270
brackets and I'll do it and I'll do the same thing with you earlier.
61
00:04:34,770 --> 00:04:38,400
Past Jason Yooralla like so.
62
00:04:39,780 --> 00:04:40,940
OK, so that's it.
63
00:04:41,360 --> 00:04:46,230
Now we have the ability to create an image model with the default constructor right here where we're
64
00:04:46,230 --> 00:04:50,490
going to call new image model and directly pass in in Edina, YORO.
65
00:04:50,790 --> 00:04:56,040
But we also have the ability to create a new image model from some JSON where we just pass in this big
66
00:04:56,040 --> 00:04:57,360
blob of JSON data.
67
00:04:58,360 --> 00:05:04,780
So now back up here, we can make use of that new constructor, so rather than saying new image model
68
00:05:04,780 --> 00:05:12,010
and passing in all that JSON, we're going to say new image model DOT from JSON like so.
69
00:05:13,210 --> 00:05:18,610
So now the result is the same as it was before we passing this JSON data, but we don't have to like
70
00:05:18,610 --> 00:05:20,820
iterate all through all those different properties.
71
00:05:21,040 --> 00:05:24,700
We only had to do that one time when we set up this named constructor down here.
72
00:05:25,730 --> 00:05:32,060
After we call from Jason, we get back our new instance of image model, we assign it to the image model
73
00:05:32,180 --> 00:05:35,580
variable and then we can freely access the different properties on it.
74
00:05:36,080 --> 00:05:37,490
So now I will run this again.
75
00:05:38,840 --> 00:05:40,970
And we should still see our oil up here, up here.
76
00:05:44,300 --> 00:05:49,550
And come on, I know this thing wants to run is just being a little bit hesitant.
77
00:05:51,390 --> 00:05:53,230
Let's try let's try a different property.
78
00:05:53,400 --> 00:05:53,790
There we go.
79
00:05:53,820 --> 00:05:55,520
OK, you are very good.
80
00:05:55,530 --> 00:05:56,890
And we can do it as well.
81
00:06:00,280 --> 00:06:01,810
And we'll get back once again.
82
00:06:02,340 --> 00:06:05,370
OK, so that's it, that's how we handle JSON data.
83
00:06:05,710 --> 00:06:10,420
We get back some rodgerson from maybe making an API request or whatever it might be.
84
00:06:10,960 --> 00:06:16,570
We call it JSON code, and then we will very commonly make a model class.
85
00:06:17,020 --> 00:06:21,730
We might add a constructor to it called a named constructor that will take that big blob of JSON.
86
00:06:21,880 --> 00:06:25,810
It'll extract the values we care about in assign it to the new instance of the model.
87
00:06:26,470 --> 00:06:31,210
It's now that we have a better idea of how to work with JSON will flip back over to a real project and
88
00:06:31,210 --> 00:06:33,790
we're going to put this image model class to work.
89
00:06:34,000 --> 00:06:35,980
So quick break and we'll see you in just a minute.
9333
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.