13.5 - JavaScript Object Notation (JSON)

دوره: Using Python to Access Web Data / فصل: JSON and the REST Architecture (Chapter 13) / درس 1

13.5 - JavaScript Object Notation (JSON)

توضیح مختصر

He's got sort of a dry sense of humor and I encourage you to watch the interview that I did with Douglas Crockford. The last few years, like I say, because of so much data is being moved between the browsers and back ends and JSON is pretty much the serialization format that's being used there. And so what happens is is that this JSON is not as rich a representation as XML, but because it's simple and it maps directly onto the internal structures that we have in all these languages, it's a lot easier to use.

  • زمان مطالعه 9 دقیقه
  • سطح خیلی سخت

دانلود اپلیکیشن «زوم»

این درس را می‌توانید به بهترین شکل و با امکانات عالی در اپلیکیشن «زوم» بخوانید

دانلود اپلیکیشن «زوم»

فایل ویدیویی

متن انگلیسی درس

So now we’re going to talk about a new serialization format. We’ve talked about XML, which is kind of complex. And there’s a simple one is JSON and it’s increasingly popular to the point where it’s entirely possible you might go a whole career and not really run into XML. XML is not necessarily bad, it’s just very powerful and more powerful than we need in simple situations. And so here’s one of the videos that I that I’ll show you. It’s a video of a fellow named Douglas Crockford and he’s credited with discovering JSON. JSON really is not a new format. JSON was invented by the constant syntax, the literal notation for constants in JavaScript for both arrays of variable constants, values, and objects. And that’s what it is. And so he makes it really clear that he just noticed that this could be a way for us to serialize data. So when I talked about serialization, I talked about how we’re not going to use the Python syntax, we’re not going to use the Java syntax, we’re going to use XML in the middle. But what happens in JSON is we’re not going use the Python syntax, we’re not going use the Java syntax, but we are going to use the JavaScript syntax in the middle. So JSON is very native to JavaScript. And interestingly, because so many things are being built in browsers these days, JavaScript is becoming an increasingly interesting language. And so JSON’s popularity is not driven so much because of JavaScript, but it certainly doesn’t hurt to have JavaScript become increasingly increasingly popular and JSON coming along with it. So Douglas Crockford is an interesting fellow. He’s got sort of a dry sense of humor and I encourage you to watch the interview that I did with Douglas Crockford. As part of the dry sense of humor, you can see two books here written from O’Reilly. One is the JavaScript Definitive Guide and then there is another book that’s almost a joke, which is I mean, it’s a very good book called JavaScript: The Good Parts. And the thickness of the parts of JavaScript that are there that are not the good parts, as Crockford would say, and so this is a Crockford book. And so this is just kind of Crockford’s wry sense of humor that, about what he thinks about JavaScript. Now Crockford would say, and I would agree, that the good parts of JavaScript make it a really great programming language, you just have to be careful not to do the stuff that he considers the “not such good parts” of JavaScript. And so Crockford basically established JSON as a standard sort of by fiat, he didn’t ask anybody’s permission and he registered json.org and put a couple of documents up that said this is what JSON is. And it’s kind of taken the world by storm. The last few years, like I say, because of so much data is being moved between the browsers and back ends and JSON is pretty much the serialization format that’s being used there. XML is still used in some situations. And so here is the similar kind of problem that we solved except we’re going to use JSON now. And so just like in all of Python, we import something. We import the JSON library. We’re going to use the triple-quoted string again. And again, we’re not going to pull this data. Normally, we’d pull this data from the Internet with urllib, or open it into a file, or whatever. And it starts with a curly brace. We’ll see another syntax where it starts with a square brace. Curly brace is like a dictionary, right? And it’s kind of like a dictionary. In JavaScript, it’s called an object, but it’s very much like a Python dictionary in that we have key-value pairs, there is a series of keys, a key followed by a colon; the keys are strings, the values are can be anything, not just a string - phone. And now here, we have a dictionary within a dictionary. So this is the phone keyword in the outer one, phone, email. and name are in the outer one. And then there’s a child dictionary here that starts from here to here and it has two things, one is type and one is number. And then this one has a child that has hide as yes. And you could think of this as a tree as well, if you really wanted to. You know, we could have this outer thing which has a name, a phone. and email. And under phone, we have type and number. Sorry, I have to make these long. And then we have hide. Right? Now, one of the things that’s different between JSON is there’s no start tag and there’s no end tag and there’s no attributes. And so this I’m kind of cheating and making this attribute and moving it down. And there’s no text area. So it’s got some different things that are sort of different nature than XML. But it has some things that we absolutely love. And that is. so we have this blob of text that we got from somewhere and we’re going to pass it in. This loads stands for load from string. So use the JSON library method or function loads from the JSON library and that does all the parsing and looks all these things up. This might blow up, it might give you traceback if there’s a syntax error, like you forgot that quote or something like, so you get a traceback here. Or not, assuming it all works well. And in this particular example, it’s going to work well. We get back an object that sort of represents, internally, all of this data. Now the interesting thing is the thing we get back is really a Python dictionary. So this ends up being a type Python dictionary as if you built this Python dictionary with name, phone, and email as keywords and then the values are Chuck. And then another dictionary and another dictionary. Okay. And so info is a dictionary. So when we talk to this dictionary, we just use the brackets syntax sub name. If we print that out, if we do info sub name, it is this. So there’s no, like, pull out the text thing, do a get, find it. There is a dictionary and it’s got a key named name. And that’s what you get back. If we are going to go find this hide, we go get info sub email, which is a dictionary, which is then gives us back this thing right here, which is also a dictionary. And so then we say sub hide within that and that pulls out the yes to print it out. So this is one of the things that works really well with JSON, is because its structure is more like the kinds of structures that we have of lists and dictionaries. And every programming language has something that’s roughly equivalent to a list and roughly equivalent to a dictionary. And so what happens is is that this JSON is not as rich a representation as XML, but because it’s simple and it maps directly onto the internal structures that we have in all these languages, it’s a lot easier to use. And it’s not as rich, but in its simplicity, it makes it so much easier to use. And so if you just compare this to the previous XML example with the gets and the dot text and like, what am I doing here, this is a lot more direct. You look at it, you write this stuff and you’re done with it. Here is an example of a list. So we’ve got lists and dictionaries and they can be nested in any way you want. So what we have here is our JSON starts here and ends here. Because it’s a square bracket, it’s a list. And so that is a list. In this case, it’s two items. And this is a dictionary. And this is a dictionary. So we end up with dictionary and dictionary. We end up with a list of two dictionaries. And so when we take this and run it through loads, assuming that there’s no syntax errors, we get back a info is a list. It’s not an object, it’s a list. And we can say because it’s a list, we can ask how many are there and that’ll be there’s going to be two things in there. And then we can write a for loop we can write a for loop that has the stuff in here and the stuff in there. item is going to iterate through those two things, which it’s going to iterate through here and then it’s going to iterate through that. This is going to be a dictionary and this is going to be a dictionary. So this loop is going to run twice, loop’s going to run twice, once with this as item, which is a dictionary, and the next time with that as item as a dictionary. And then inside the loop so item points to this and we say item sub name. well, items sub name is just this Chuck. And then item sub id is 0.01. And item sub x is 2. And again, if you look back at the XML, it does the same thing. You see, it’s just a lot more complex. Right? A lot more complex. JSON is simpler, so the code we write to work with JSON is simpler and that’s why a lot of people like it. A lot of people would say, you know, what you should do is if you can use JSON, you probably should use JSON, but if you have something more complex, you might want to use XML. But these days, there’s sort of a strong movement towards JSON. And so that gives us a summary of how JSON works. And now, we’re going to talk move up a little bit and talk about the Service Oriented Approach.

مشارکت کنندگان در این صفحه

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

🖊 شما نیز می‌توانید برای مشارکت در ترجمه‌ی این صفحه یا اصلاح متن انگلیسی، به این لینک مراجعه بفرمایید.