使用Python从嵌套的JSON对象中提取数据 [英] Surgically extracting datum from nested JSON object using Python
本文介绍了使用Python从嵌套的JSON对象中提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从这个嵌套的JSON对象中,我需要解析并漂亮地打印"id"
的值和"location"
的值.
From this nested JSON object I need to parse out and pretty print the value for "id"
and the value for "location"
.
{
"links": {
"self": "http://localhost:2510/api/v2/jobs?skills=data%20science"
},
"data": [
{
"id": 121,
"type": "job",
"attributes": {
"title": "Data Scientist",
"date": "2014-01-22T15:25:00.000Z",
"description": "Data scientists are in increasingly high demand amongst tech companies in London. Generally a combination of business acumen and technical skills are sought. Big data experience ..."
},
"relationships": {
"location": {
"links": {
"self": "http://localhost:2510/api/v2/jobs/121/location"
},
"data": {
"type": "location",
"id": 3
}
},
"country": {
"links": {
"self": "http://localhost:2510/api/v2/jobs/121/country"
},
"data": {
"type": "country",
"id": 1
}
},
我一直试图以这种方式来抓它:
I've been trying to grab it in this way:
with open('data.json') as data_file:
data = json.load(data_file)
for item in data["data"]:
for job in data['id']:
for title in data['data']:
print(title.get('location')
但是我无法获取所需的数据.
but I've not been able to grab the data I need.
如何仅提取我感兴趣的数据?
How can I extract only those datum that I'm interested in?
我一直在尝试进行这种探索,但是即使在文件完成之前也会崩溃:
I've been trying this as some kind of exploration, but even this crashes before the file finishes:
import json
from pprint import pprint
with open('data.json') as data_file:
data = json.load(data_file)
for item in data["data"]:
for job in item:
print( job )
推荐答案
我正在飞跃,最后猜测出您真正想要的信息,这是每个位置ID的项目ID的列表:
I'm taking a leap and guessing the info you really want in the end, which is a list of item IDs for each location ID:
import json
from collections import defaultdict
with open('prettyPrint.txt') as data_file:
data = json.load(data_file)
locations = defaultdict(int)
for item in data['data']:
location = item['relationships']['location']['data']['id']
locations[location] += 1
print(locations)
这篇关于使用Python从嵌套的JSON对象中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文