使用Python解析嵌套的JSON对象

时间:2018-07-09 22:00:15

标签: python json

我具有以下JSON数据,并希望使用Python提取电子邮件值:

{
  "_links": {
"self": {
  "href": "https://example.com/comments/9"
}
  },
"_embedded": {
"customer": {
  "name": "Jamie XXXX",
  "email": "jamie@example.tv",
  "thumbnail": {
    "small": "https://secure.gravatar.com/avatar/dfd.png?d=blank&r=PG&s=100",
    "medium": "https://secure.gravatar.com/avatar/dfd.png?d=blank&r=PG&s=200",
    "large": "https://secure.gravatar.com/avatar/dfdfd.png?d=blank&r=PG&s=300"
  }
},
"comments": []
},
"id": 9,
"video_id": null,
"content": "j/k I meant that as a reply",
"comments_count": 0,
"created_at": "2014-03-12T17:46:07Z",
"updated_at": "2014-03-12T17:46:07Z"
}

我尝试了类似的方法,但是它不起作用:

jsonresp = r.json()
for k, v in jsonresp:
     print(jsonresp['_embedded']['customer']['email'])

2 个答案:

答案 0 :(得分:3)

jsonresp = r.json()
print(jsonresp['_embedded']['customer']['email'])

答案 1 :(得分:0)

使用pyjq

import json
import pyjq

with open("input.json", "r") as myfile:
    data=json.load(myfile)

print pyjq.first('._embedded.customer.email', data);