تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Why Share Data Externally0:03

There are times when we want to take data from our JavaScript and share that with some other system and you know, by some other system, I mean some other system, like a PHP application running on the server. Or maybe we need to interact with a web API that's owned by somebody else, but we need to send information to that web API. Now, a long time ago, there really wasn't a good way to do

Introducing JSON Format0:24

that web API. Now, a long time ago, there really wasn't a good way to do that, but now we have a standard called JavaScript Object Notation or just JSON. I call it js. The people call it JSON. It really doesn't matter. You can call it whatever you want. But the idea is that this is a data format that we can use to take our objects from JavaScript. And it's not just, you know, simple values. It is an entire object complete

And it's not just, you know, simple values. It is an entire object complete with its original data structure, and it allows us to send that object wherever it is that we need it to go. So of course if we want to send, let's say this detail from the gameGuestEvent to the server, well we would need to convert that into JSON structure. And the way that we would do that is by calling the,

Serializing with stringify1:10

that into adjacent structure. And the way that we would do that is by calling the json_encode method. I'm not wild about this name. It was popularized by the person who actually came up with the idea of JSON, which I guess it's his right to call it whatever he wants. I think it should have been called serialized, but he didn't consult me. So here what we are essentially doing is taking our detail

but he didn't consult me. So here what we are essentially doing is taking our detail from the GameGuestEvent, and we are converting that into a string representation of that object. But it is completely a string. So this means that it's going to have the properties. In fact, let's look at this. Let's write this out to the console so that you can see what exactly happens whenever we make a guess.

Let's write this out to the console so that you can see what exactly happens whenever we make a guess. And then we take that detail and convert it into adjacent structure. So this doesn't look like a string, but it is, just imagine that we have a couple of quotes surrounding it. So this is completely a string, this entire thing. And that's what makes this a really fantastic format because we can take an entire object,

And that's what makes this a a really fantastic format because we can take an entire object, and it doesn't have to be an object, it can be an array. It can even be just a simple value like a string or a number or a Boolean. But we typically use it with objects or arrays. And we can take these structures and turn them into a string representation. But here we can see that the property, guess notice that it is a string because we have the quotes around it.

But here we can see that the property, guess notice that it is a string because we have the quotes around it. And notice it's the double quotes, not single quotes, but the property name is a string. The value looks like it's a numeric value, and it is. But remember that this is contained within a string. So it itself is still a string, but here we see the result. Property its value is the string too low? Then we have the remaining attempts property, which is denoted with the string,

Then we have the remaining attempts property, which is denoted with the string, and then its value is the numeric value of 6. So, you know, as I've said, this is an exact representation of an object in string format, and once it's in this format, we can send it wherever we need. We can even store it within the browser itself, which is something that we will look at in the next episode. Now, of course, if we can convert an object into a string,

Deserializing with parse3:25

which is something that we will look at in the next episode. Now, of course, if we can convert an object into a string, it kind of goes without saying that we can convert a string into an object. However, the, the caveat here is that the string that we are converting into an object has to be a JS structure. It has to be a valid JS structure. Otherwise it's, it's not gonna work. But here to deserialize a JS structure,

Otherwise it's, it's not gonna work. But here to deserialize a JS structure, we use the parse method. And then let's take a look at this. So if we write this out to the console, we will see first of all our JSON structure, but then we will see the object that we parse it into. So here our JSON structure, just like it was before. But now notice that the actual object itself, it has the same information.

Cloning Objects via JSON4:09

But now notice that the actual object itself, it has the same information because we essentially cloned the event detail. And in fact, that's one of the uses of json. If you wanted to make a clone of any object, you could serialize it into a json structure, then you could parse it back into another object, and it's an exact copy of the object that you had before, except that now it is completely separate from that other object.

Summary of JSON Uses4:33

before, except that now it is completely separate from that other object. So you can play with it, you can destroy it if you want to without affecting the original object. This is a very valid thing to do if you need to clone an object. So the JS object gives us two very useful methods. The first allows us to uh, essentially convert an object or an array or any JavaScript value into a string that we can then do whatever it is that we need to send it.

or an array or any JavaScript value into a string that we can then do whatever it is that we need to send it to the server, send it to some remote API, or we can store it within the browser. But then we also have another method called parse that we can use to take adjacent structure that comes from the server or comes from the remote API, or we read it from the browser so that we can convert it into an object that we can then use. It's such a simple concept,

that we can convert it into an object that we can then use. It's such a simple concept, but it has a ton of usability.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟