[英]Parsing JSON with Python from URL
So I'm trying to get json from a URl and the request works and I get the json but I'm not able to print specific things from it. 因此,我试图从URl中获取json,并且请求有效,并且获取了json,但是我无法从中打印出特定内容。
request_url = 'http://api.tumblr.com/v2/user/following?limit=1'
r = requests.get(request_url, auth=oauth).json()
r["updated"]
I'm very new with python I'm guessing I need to get the json into a array but I have no idea where to even begin. 我对python很陌生,我猜想我需要将json放入数组中,但我什至不知道从哪里开始。
According to the tumblr api I should be able to get something like this. 根据tumblr api,我应该能够得到这样的东西。
{
"meta": {
"status": 200,
"msg": "OK"
},
"response": {
"total_blogs": 4965,
"blogs": [
{
"name": "xxxxx",
"title": "xxxxxx",
"description": "",
"url": "http://xxxxxx.tumblr.com/",
"updated": 1439795949
}
]
}
}
I only need the name, url, and updated just no idea how to seperate that out. 我只需要名称,URL和更新,就不知道如何将其分开。
You should be able to print values as though it were nested arrays: 您应该能够像嵌套数组一样打印值:
r["response"]["blogs"][0]["updated"]
should get you the updated bit, don't go straight to it. r["response"]["blogs"][0]["updated"]
应该会为您提供更新的位,不要直接去找。 Just work your way down. 顺其自然吧。 Note how blogs is an array, so in a normal case you may actually want to work towards
r["response"]["blogs"]
, then loop through it and for each of those items, grab the ["updated"]
. 请注意,博客是如何构成数组的,因此在正常情况下,您实际上可能想要转向
r["response"]["blogs"]
,然后遍历整个r["response"]["blogs"]
,并为每个项目抓取["updated"]
。
Similarly, r["meta"]["msg"]
will get you the meta message. 同样,
r["meta"]["msg"]
将为您提供元消息。
Just access the levels one by one. 只需一个接一个地访问各个级别。
for i in r["response"]["blogs"]:
print i["name"],i["url"],i["updated"]
So this code can be used to print all the objects inside the blogs
list 因此,此代码可用于打印
blogs
列表中的所有对象
To explain how this works: 解释这是如何工作的:
Json objects are decoded into something called dictionaries
in Python. Json对象在Python中被解码为
dictionaries
。 Dictionaries are simple key value pairs. 字典是简单的键值对。 In your example,
在您的示例中
r is a dictionary with the following keys: r是具有以下键的字典:
meta, response
You access the value of a key using r["meta"]. 您可以使用r [“ meta”]访问键的值。
Now meta itself is a dictionary. 现在,meta本身就是一本字典。 The keys associated are:
关联的键为:
status,msg
So, r["meta"]["status"]
gives the status value returned by the request. 因此,
r["meta"]["status"]
给出了请求返回的状态值。
The JSON data gets converted as dict which is set to r
as per your code. JSON数据将转换为dict,根据您的代码设置为
r
。
For accessing the value associated with updated
key, you need to first access the values before it. 要访问与
updated
密钥关联的值,您需要先访问它的值。
You should first access r["response"]
which contains the actual response of the api. 您应该首先访问
r["response"]
,其中包含api的实际响应。 From that level, you should next access r["response"]["blogs"]
and then loop through that to find the value of the updated
key. 从该级别开始,您接下来应访问
r["response"]["blogs"]
,然后循环遍历以找到updated
键的值。
If it is a single blog, you can do something like r["response"]["blogs"][0]["updated"]
如果是单个博客,则可以执行类似
r["response"]["blogs"][0]["updated"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.