对如何访问 json 代码中的嵌套值感到困惑?
confused on how to access nested values in json code?
使用 pytumblr 客户端我得到了这种类型的输出
{
"total_blogs": 2,
"blogs": [
{
"name": "testblog",
"title": "testblog",
"description": "A testblog",
"url": "https://testblog.tumblr.com/",
"uuid": "t:g8wqt6wBUe3AJkJXYHn1",
"updated": 1526680515
},
{
"name": "testblog1",
"title": "testblog1",
"description": "A testblog1",
"url": "https://testblog1.tumblr.com/",
"uuid": "t:qwuedBBFIPMTViKhjozp",
"updated": 1510382395
}],
"_links": {
"next": {
"href": "/v2/user/following?offset=20",
"method": "GET",
"query_params": {
"offset": "20"
}
}
}
}
}
我可以很好地打印 total_blogs 和博客的值,但我无法访问内部值,特别是 url,而且我无法应用教程或我在这里看到的一些其他示例,这些示例有助于解决此问题。
最后的游戏基本上就是能够循环程序,直到我获得所有 url 值。我一次只能访问 20 个博客,所以这就是我必须获得的 url 个值
此页面中的答案似乎是解决方案,但试图将其应用到我的代码中
for anything in usrFollowing:
if isinstance(usrFollowing[anything], list):
for values in usrFollowing[anything]:
print(values['blogs']['name'])
只是从最后一行给我 KeyError: 'blogs' 。我不确定此时我还能做什么
我遇到的另一个问题是弄清楚如何将代码输出为更易读的格式。在 tumblr 网站控制台上,它的输出类似于我上面显示的初始代码,但我得到的只是打印到控制台的正在进行的行。有什么办法可以改变这个吗?
这应该检索您的 tumblr 关注的博客网址:
import pytumblr
client = pytumblr.TumblrRestClient(...) # replace ... with your credentials
usrFollowing = client.following()
for blog in usrFollowing['blogs']:
print(blog['url'])
我认为理解这个结构的方法是一次一片。
usrFollowing
var 是一个字典
...
usrFollowing = client.following()
for key in usrFollowing.keys():
print(key)
# outputs:
# blogs
# _links
# total_blogs
因此,要访问每个博客,我们可以使用键 blogs
:
遍历它们
...
usrFollowing = client.following()
for blog in usrFollowing['blogs']:
print(blog)
# outputs something like:
# {u'updated': 1539793245, u'uuid': u't:CwoihvyyOxn8Mk5TUS0KDg', u'title': u'Tumblr Engineering', u'url': u'https://engineering.tumblr.com/', u'name': u'engineering', u'description': u'Dispatches from the intrepid tinkerers behind technology at Tumblr.'}
# {u'updated': 1545058816, u'uuid': u't:0aY0xL2Fi1OFJg4YxpmegQ', u'title': u'Tumblr Staff', u'url': u'https://staff.tumblr.com/', u'name': u'staff', u'description': u''}
有几种方法可以以更 "human" 的格式输出对象,使用 pprint
或将对象转换为 JSON 并指定缩进量:
...
import pprint
import json
print('Python pretty-printed')
for blog in usrFollowing['blogs']:
pprint.pprint(blog)
print('')
print('JSON pretty-printed')
for blog in usrFollowing['blogs']:
print(json.dumps(blog, indent=2))
# outputs something like:
# Python pretty-printed
# {u'description': u'Dispatches from the intrepid tinkerers behind technology at Tumblr.',
# u'name': u'engineering',
# u'title': u'Tumblr Engineering',
# u'updated': 1539793245,
# u'url': u'https://engineering.tumblr.com/',
# u'uuid': u't:CwoihvyyOxn8Mk5TUS0KDg'}
# {u'description': u'',
# u'name': u'staff',
# u'title': u'Tumblr Staff',
# u'updated': 1545058816,
# u'url': u'https://staff.tumblr.com/',
# u'uuid': u't:0aY0xL2Fi1OFJg4YxpmegQ'}
#
# JSON pretty-printed
# {
# "updated": 1539793245,
# "uuid": "t:CwoihvyyOxn8Mk5TUS0KDg",
# "title": "Tumblr Engineering",
# "url": "https://engineering.tumblr.com/",
# "name": "engineering",
# "description": "Dispatches from the intrepid tinkerers behind technology at Tumblr."
# }
# {
# "updated": 1545058816,
# "uuid": "t:0aY0xL2Fi1OFJg4YxpmegQ",
# "title": "Tumblr Staff",
# "url": "https://staff.tumblr.com/",
# "name": "staff",
# "description": ""
# }
这些词典有一个 url
键,因此您可以使用以下命令打印它们:
...
usrFollowing = client.following()
for blog in usrFollowing['blogs']:
print(blog['url'])
# outputs something like:
# https://engineering.tumblr.com/
# https://staff.tumblr.com/
使用 pytumblr 客户端我得到了这种类型的输出
{
"total_blogs": 2,
"blogs": [
{
"name": "testblog",
"title": "testblog",
"description": "A testblog",
"url": "https://testblog.tumblr.com/",
"uuid": "t:g8wqt6wBUe3AJkJXYHn1",
"updated": 1526680515
},
{
"name": "testblog1",
"title": "testblog1",
"description": "A testblog1",
"url": "https://testblog1.tumblr.com/",
"uuid": "t:qwuedBBFIPMTViKhjozp",
"updated": 1510382395
}],
"_links": {
"next": {
"href": "/v2/user/following?offset=20",
"method": "GET",
"query_params": {
"offset": "20"
}
}
}
} }
我可以很好地打印 total_blogs 和博客的值,但我无法访问内部值,特别是 url,而且我无法应用教程或我在这里看到的一些其他示例,这些示例有助于解决此问题。
最后的游戏基本上就是能够循环程序,直到我获得所有 url 值。我一次只能访问 20 个博客,所以这就是我必须获得的 url 个值
此页面中的答案似乎是解决方案,但试图将其应用到我的代码中
for anything in usrFollowing:
if isinstance(usrFollowing[anything], list):
for values in usrFollowing[anything]:
print(values['blogs']['name'])
只是从最后一行给我 KeyError: 'blogs' 。我不确定此时我还能做什么
我遇到的另一个问题是弄清楚如何将代码输出为更易读的格式。在 tumblr 网站控制台上,它的输出类似于我上面显示的初始代码,但我得到的只是打印到控制台的正在进行的行。有什么办法可以改变这个吗?
这应该检索您的 tumblr 关注的博客网址:
import pytumblr
client = pytumblr.TumblrRestClient(...) # replace ... with your credentials
usrFollowing = client.following()
for blog in usrFollowing['blogs']:
print(blog['url'])
我认为理解这个结构的方法是一次一片。
usrFollowing
var 是一个字典
...
usrFollowing = client.following()
for key in usrFollowing.keys():
print(key)
# outputs:
# blogs
# _links
# total_blogs
因此,要访问每个博客,我们可以使用键 blogs
:
...
usrFollowing = client.following()
for blog in usrFollowing['blogs']:
print(blog)
# outputs something like:
# {u'updated': 1539793245, u'uuid': u't:CwoihvyyOxn8Mk5TUS0KDg', u'title': u'Tumblr Engineering', u'url': u'https://engineering.tumblr.com/', u'name': u'engineering', u'description': u'Dispatches from the intrepid tinkerers behind technology at Tumblr.'}
# {u'updated': 1545058816, u'uuid': u't:0aY0xL2Fi1OFJg4YxpmegQ', u'title': u'Tumblr Staff', u'url': u'https://staff.tumblr.com/', u'name': u'staff', u'description': u''}
有几种方法可以以更 "human" 的格式输出对象,使用 pprint
或将对象转换为 JSON 并指定缩进量:
...
import pprint
import json
print('Python pretty-printed')
for blog in usrFollowing['blogs']:
pprint.pprint(blog)
print('')
print('JSON pretty-printed')
for blog in usrFollowing['blogs']:
print(json.dumps(blog, indent=2))
# outputs something like:
# Python pretty-printed
# {u'description': u'Dispatches from the intrepid tinkerers behind technology at Tumblr.',
# u'name': u'engineering',
# u'title': u'Tumblr Engineering',
# u'updated': 1539793245,
# u'url': u'https://engineering.tumblr.com/',
# u'uuid': u't:CwoihvyyOxn8Mk5TUS0KDg'}
# {u'description': u'',
# u'name': u'staff',
# u'title': u'Tumblr Staff',
# u'updated': 1545058816,
# u'url': u'https://staff.tumblr.com/',
# u'uuid': u't:0aY0xL2Fi1OFJg4YxpmegQ'}
#
# JSON pretty-printed
# {
# "updated": 1539793245,
# "uuid": "t:CwoihvyyOxn8Mk5TUS0KDg",
# "title": "Tumblr Engineering",
# "url": "https://engineering.tumblr.com/",
# "name": "engineering",
# "description": "Dispatches from the intrepid tinkerers behind technology at Tumblr."
# }
# {
# "updated": 1545058816,
# "uuid": "t:0aY0xL2Fi1OFJg4YxpmegQ",
# "title": "Tumblr Staff",
# "url": "https://staff.tumblr.com/",
# "name": "staff",
# "description": ""
# }
这些词典有一个 url
键,因此您可以使用以下命令打印它们:
...
usrFollowing = client.following()
for blog in usrFollowing['blogs']:
print(blog['url'])
# outputs something like:
# https://engineering.tumblr.com/
# https://staff.tumblr.com/