Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,100 --> 00:00:05,210
In the last section, we took a look at the API that we're going to use to fetch some images to show
2
00:00:05,210 --> 00:00:09,950
on the screen to the user, and each time we make a request to get a individual image, we get back
3
00:00:09,950 --> 00:00:11,350
a little bit of JSON data.
4
00:00:11,720 --> 00:00:15,920
So we're going to very quickly look at a dart pad example and get a better idea of how to work with
5
00:00:15,920 --> 00:00:16,460
JSON.
6
00:00:17,150 --> 00:00:19,720
I'm going to flip on over to a dart pad instance.
7
00:00:19,850 --> 00:00:25,090
Remember, you can get here by going to dart pad dot dart langue than inside of here.
8
00:00:25,100 --> 00:00:27,860
I'm going to immediately create a main function.
9
00:00:28,770 --> 00:00:33,690
Now, we're not going to actually fetch any data from dart pad, dart pad does not allow us to make
10
00:00:33,690 --> 00:00:35,300
any requests.
11
00:00:35,610 --> 00:00:39,330
So instead, I'm just going to manually write out a little snippet of JSON.
12
00:00:39,600 --> 00:00:42,780
So make a variable called raw JSON.
13
00:00:43,590 --> 00:00:47,100
I'll put down a set of single quotes, make sure you have single as well.
14
00:00:47,610 --> 00:00:53,070
And then inside of here I'll put a object, I'll put a double quote for the user's property.
15
00:00:53,880 --> 00:00:55,740
Make sure you got double quotes around your URL.
16
00:00:56,820 --> 00:01:03,720
And then I'll just put some fake you URL in there, like HTP, Colon, Seglora JPEG.
17
00:01:04,730 --> 00:01:10,340
And I'll close off the quote and then I will give this thing an ID property as well, so I'll put down
18
00:01:10,340 --> 00:01:10,850
a comma.
19
00:01:12,310 --> 00:01:17,440
Another quote around it, and I'll give it an idea of how about one that's reasonable?
20
00:01:18,400 --> 00:01:23,980
So notice that I have double quotes around you, URL, double quotes around the actual URL, double
21
00:01:23,980 --> 00:01:29,410
quotes around it, but then the number right here is a integer, so no quotes around it.
22
00:01:31,280 --> 00:01:35,900
All right, so we can kind of imagine that maybe this is some Rodgerson data that we have fetch from
23
00:01:35,900 --> 00:01:42,350
some remote API, so we need to now pass this JSON into something that we can work with, with darts.
24
00:01:43,130 --> 00:01:48,510
In order to pass JSON, we're going to import a dart standard library package called Convert.
25
00:01:49,130 --> 00:01:56,000
So at the top of the file, I'm going to add in an import statement and I'll say dart colon convert
26
00:01:56,120 --> 00:01:56,670
like so.
27
00:01:57,530 --> 00:01:59,830
So this is another type of import statement.
28
00:01:59,840 --> 00:02:04,700
Remember, we've already seen import statements for importing from packages from files that you and
29
00:02:04,700 --> 00:02:05,170
I write.
30
00:02:05,300 --> 00:02:09,530
And this is the third type when we try to import from a dart standard library.
31
00:02:10,639 --> 00:02:18,290
Within this convert package is a function called JSON or axemen, not a function, but an object, that
32
00:02:18,290 --> 00:02:23,810
JSON object has functions tied to it that allow us to process projects on data like this right here.
33
00:02:24,500 --> 00:02:30,410
So to take this data and turn it into something that we can actually access inside of dirt, we can
34
00:02:30,410 --> 00:02:36,440
write out JSON, dot, decode, and then we'll pass in that rodgerson.
35
00:02:38,160 --> 00:02:44,250
Now, when I do that, you possibly will see Jason light up in red right here and it might say undefined
36
00:02:44,250 --> 00:02:46,700
name Jason, that is totally OK.
37
00:02:46,750 --> 00:02:50,430
That's because Dart Pad is not properly picking up the import statement up here.
38
00:02:50,920 --> 00:02:56,220
So if you have your code exactly like mine and you see Jason with the red underline totally fine, your
39
00:02:56,220 --> 00:02:57,430
code is still going to work.
40
00:02:57,480 --> 00:03:00,480
This is an error on dart pad side, not ours.
41
00:03:01,630 --> 00:03:05,920
So we're going to take the results of that function, call and assign it to a variable that we'll call
42
00:03:06,190 --> 00:03:07,990
past Jason like so.
43
00:03:08,950 --> 00:03:14,080
And now we can print past Jason and if we run this example.
44
00:03:17,480 --> 00:03:22,370
We will see something like this up here now, I'm sure that it looks almost identical to what we had
45
00:03:22,370 --> 00:03:29,390
before, but is now inside of a data structure that we call a map, a map and dart is identical to a
46
00:03:29,390 --> 00:03:34,730
object in JavaScript or a hash in Ruby or maps in Java, of course, as well.
47
00:03:35,640 --> 00:03:43,590
So the past JSON object right here is a map data structure that has keys of Eurail and ID and then a
48
00:03:43,590 --> 00:03:50,610
value of this URL right here and the integer one to look at values inside of this map object, we can
49
00:03:50,610 --> 00:03:56,040
put on square brackets like so and then in a string, add in the property that we want to look up.
50
00:03:56,580 --> 00:04:02,040
So if I want to retrieve the value of the U.S. looking right here, I can enter a new URL like so.
51
00:04:02,550 --> 00:04:05,130
And now if I run this example, I'll see just that.
52
00:04:05,130 --> 00:04:06,270
You are I'll get printed out.
53
00:04:07,470 --> 00:04:09,600
And of course, we could do the I.D. as well.
54
00:04:13,130 --> 00:04:19,940
And we get back one, OK, so that is handling Jason in a nutshell, and there's just one little thing
55
00:04:19,940 --> 00:04:20,820
I want to mention here.
56
00:04:21,560 --> 00:04:27,440
Remember, when we work with DART, it is a statically typed language and wherever possible, we really
57
00:04:27,440 --> 00:04:31,170
like to add in types to describe the type of data that we are working with.
58
00:04:31,850 --> 00:04:38,270
So in this case, when we pass this JSON data right here, DART has really no idea what we are getting
59
00:04:38,270 --> 00:04:38,690
back.
60
00:04:39,170 --> 00:04:40,760
So, yeah, Dart's going to know that.
61
00:04:40,760 --> 00:04:45,350
Sure, it's a map, it contains some amount of information inside of it, but it doesn't really know
62
00:04:45,350 --> 00:04:49,800
the different properties and the types of those different property values.
63
00:04:50,300 --> 00:04:56,570
So Dart doesn't really know at compile time that your URL is going to contain a string and it is going
64
00:04:56,570 --> 00:04:57,680
to contain an integer.
65
00:04:58,230 --> 00:05:05,150
So one thing that you and I commonly do as developers is we create what's called a model class to give
66
00:05:05,160 --> 00:05:09,830
DART a better idea of the type of data that's going to come back from our Jason endpoint.
67
00:05:10,520 --> 00:05:12,110
Let's take a quick pause right here.
68
00:05:12,260 --> 00:05:17,300
We'll come back in the next section and we'll talk about how we can create a model class to contain
69
00:05:17,300 --> 00:05:22,580
the JSON data that we just fetch and give DART a better idea of the type of data that we are working
70
00:05:22,580 --> 00:05:22,850
with.
71
00:05:23,210 --> 00:05:25,030
So quick break and I'll see you in just a moment.
7427
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.