繁体   English   中英

使用Python解析嵌套的JSON对象

[英]Parsing nested JSON objects with Python

我有以下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'])
jsonresp = r.json()
print(jsonresp['_embedded']['customer']['email'])

使用pyjq

import json
import pyjq

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM