Worked Example- JSON (Chapter 13)
دوره: Using Python to Access Web Data / فصل: JSON and the REST Architecture (Chapter 13) / درس 2سرفصل های مهم
Worked Example- JSON (Chapter 13)
توضیح مختصر
Which turns this set of curly braces, spaces, commas and perhaps syntax errors into a structured object. Now the difference between XML and json is that this turns into a Python dictionary with key value pairs, okay? And so I'll put my little square brace back in so it's not syntactically broken.
- زمان مطالعه 0 دقیقه
- سطح متوسط
دانلود اپلیکیشن «زوم»
فایل ویدیویی
برای دسترسی به این محتوا بایستی اپلیکیشن زبانشناس را نصب کنید.
ترجمهی درس
متن انگلیسی درس
Hello, everybody.
Welcome to Python for Everybody.
We’re playing with the web services chapter right now.
And if you want to get the materials for this course you can go here and download the sample zip.
Samplecode.zip, I’ve got this all sitting already on my computer.
And I also have the whole thing in GitHub if you want to get it out of GitHub.
So the thing we’re talking about now, is we’re talking about the json1.py example from the book.
And so json is kind of like XML except a lot simpler.
And that’s why a lot of people like it.
It’s not that json is always better, but json is better in a lot of situations that don’t require the complexity of xml.
So if we start to import json, json is built into Python but we have to ask to import it.
Again we’re using a triple quoted string to put the json in there.
And json looks a lot like Python dictionaries, key value pairs, key value pairs.
In this case this is a key and the value itself is another dictionary or in JSON terms an object but again key value pairs.
Within key value pairs, within key value pairs.
And all these little curly brace guys have to line up properly.
And so, like time, this is a string which we normally would read and decode from the internet.
But for now, we’re just going to have it in there.
Load json dot loads says go into the json library.
Pull out load string and parse this.
Which turns this set of curly braces, spaces, commas and perhaps syntax errors into a structured object.
And if we had made a syntax error in here then this would blow up.
But if this doesn’t make a syntax error, if this doesn’t blow up then we have a structured representation.
Now the difference between XML and json is that this turns into a Python dictionary with key value pairs, okay?
And so once we have this, this is a dictionary.
And we can say info[“name”].
And that’s the exact syntax that we would use to get the dictionary and that’s going to extract this value out of there.
And if we want to go in deeper we can say info sub email, and that’s what info sub email is right there.
And then sub hide.
So that’s a dictionary within a dictionary.
And so if we run this, Python3 json1.py it digs in really fast.
So this is why people tend to like JSON.
Because you read the JSON, which is actually a syntax derived from JavaScript, but it looks just like the syntax for a Python.
So that’s moving an object, a JSON object that turns directly into a Python dictionary with nested dictionaries.
Now we’re going to look at JSON too.
And so JSON too, we’re going to see a list, an array in JSON terms, but it turns into a list in Python terms. So this is a list of dictionaries.
In JavaScript, that would be an array of objects, but in Python it’s a list of dictionaries, so we’ll just pretend that it’s a list of dictionaries.
Again, we load the string, parsing, looking for syntax errors.
So let’s just make a syntax error here and run python json2.py and you’ll see where it blows up.
It blows up at line 15 which is right here.
This loads blows up.
Now you could put a around it to save it but we’re not going to do that.
And it even complains.
It says, look, we’re expecting something here in line 11, and that’s line 11 of the json which starts at line 4.
And so I’ll put my little square brace back in so it’s not syntactically broken.
So let’s run it again and make sure that she runs, and yes she does.
So this parses it, and converts from the json syntax into a Python in this case list.
Because it’s got square braces instead of curly braces.
The previous example had square braces.
And we can then take a of it, and it’s an array, it’s a list.
And we see that there are two things in there.
And then we’re going to iterate through and this item is going to iterate through these dictionaries, that dictionary followed by that dictionary.
So the first time it’s item, sub, name, which is this value right here and then item, subID, which is this value.
So you can dig right in to this, but you’re not using get and you’re not using the weird extra find or find all or anything.
You just are going at this structures directly.
And so you can quickly extract this stuff out.
And we read through, id name is Chuck.
Whoops, name is Chuck.
There are no attributes by the way.
X is 2 and so we had to make x.
So if you look at the XML we had this concept of attributes on the outer tag.
These things are also not named.
We just have to know what we’re looking for.
So JSON represents simple structures but it’s much simpler to use.
So I hope this has been useful to you and talk to you in a bit about some more JSON.
مشارکت کنندگان در این صفحه
تا کنون فردی در بازسازی این صفحه مشارکت نداشته است.
🖊 شما نیز میتوانید برای مشارکت در ترجمهی این صفحه یا اصلاح متن انگلیسی، به این لینک مراجعه بفرمایید.