f = open("sample_diction.json","r")
sample_photo_rep = json.loads(f.read())
print sample_photo_rep
f.close()
上面是我的代码正在打开文件sample_diction.json并将其内容作为Python对象加载到变量sample_photo_rep中。我要做的下一步是编写代码来访问sample_flickr_obj中的嵌套数据,以创建该照片的所有标记列表(“sample_diction.json”中的数据)。然后我想在一个名为sample_tags_list的变量中保存标签列表。 下面是sample_diction.json中包含的数据....我不知道从哪里开始。任何帮助,将不胜感激。
{
"photo": {
"people": {
"haspeople": 0
},
"dateuploaded": "1467709435",
"owner": {
"username": "Ansel Adams",
"realname": "",
"nsid": "48093195@N03",
"iconserver": "7332",
"location": "",
"path_alias": null,
"iconfarm": 8
},
"publiceditability": {
"canaddmeta": 1,
"cancomment": 1
},
"id": "27820301400",
"title": {
"_content": "Photo1"
},
"media": "photo",
"tags": {
"tag": [
{
"machine_tag": false,
"_content": "nature",
"author": "48093195@N03",
"raw": "Nature",
"authorname": "ac | photo albums",
"id": "48070141-27820301400-5470"
},
{
"machine_tag": false,
"_content": "mist",
"author": "48093195@N03",
"raw": "Mist",
"authorname": "ac | photo albums",
"id": "48070141-27820301400-852"
},
{
"machine_tag": false,
"_content": "mountain",
"author": "48093195@N03",
"raw": "Mountain",
"authorname": "ac | photo albums",
"id": "48070141-27820301400-1695"
}
]
},
"comments": {
"_content": "0"
},
"secret": "c86034becf",
"usage": {
"canblog": 0,
"canshare": 1,
"candownload": 0,
"canprint": 0
},
"description": {
"_content": ""
},
"isfavorite": 0,
"views": "4",
"farm": 8,
"visibility": {
"isfriend": 0,
"isfamily": 0,
"ispublic": 1
},
"rotation": 0,
"dates": {
"taken": "2016-07-05 11:03:52",
"takenunknown": "1",
"takengranularity": 0,
"lastupdate": "1467709679",
"posted": "1467709435"
},
"license": "0",
"notes": {
"note": []
},
"server": "7499",
"safety_level": "0",
"urls": {
"url": [
{
"type": "photopage",
"_content": "https://www.flickr.com/photos/48093195@N03/27820301400/"
}
]
},
"editability": {
"canaddmeta": 0,
"cancomment": 0
}
},
"stat": "ok"
}
答案 0 :(得分:0)
嵌套数据一开始可能看起来令人生畏,但如果你一步一步走的话,这很容易。
您从sample_photo_re
开始。这是一个词典,而且只有一把钥匙。
所以:
sample_photo_rep["photo"]
这是另一个词。有趣的信息似乎在tags
:
sample_photo_rep["photo"]["tags"]
又一个字典。所以:
sample_photo_rep["photo"]["tags"]["tag"]
这一次,它是一个列表,所以你可以迭代它:
for tag in sample_photo_rep["photo"]["tags"]["tag"]:
print tag
输出:
{'machine_tag': False, '_content': 'nature', 'author': '48093195@N03', 'raw': 'Nature', 'authorname': 'ac | photo albums', 'id': '48070141-27820301400-5470'}
{'machine_tag': False, '_content': 'mist', 'author': '48093195@N03', 'raw': 'Mist', 'authorname': 'ac | photo albums', 'id': '48070141-27820301400-852'}
{'machine_tag': False, '_content': 'mountain', 'author': '48093195@N03', 'raw': 'Mountain', 'authorname': 'ac | photo albums', 'id': '48070141-27820301400-1695'}
这些都是决定性的。您可能只对raw
密钥感兴趣,所以:
for tag in sample_photo_rep["photo"]["tags"]["tag"]:
print tag['raw']
# Nature
# Mist
# Mountain
完成!
如果您收到错误(例如KeyError: 'row'
),请退一步查看数据,可能会使用type(object)
查看数据是否为列表或字典。
更新:获取[u'nature', u'mist', u'mountain']
:
[unicode(tag) for tag in sample_photo_rep["photo"]["tags"]["tag"]]