pythonjson多层嵌套取值,使⽤Python从嵌套JSON对象中提取
长路迢迢数据
From this nested JSON object I need to par out and pretty print the value for "id" and the value for "location".
{
"links": {
"lf": "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": {
"lf": "localhost:2510/api/v2/jobs/121/location"
},
"data": {
"type": "location",
"id": 3
}
},
"country": {
"links": {
"lf": "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']:
下水大佛
('location')
but I've not been able to grab the data I need.
How can I extract only tho datum that I'm interested in?
Edit
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 )
解决⽅案
形容人外貌的四字词语>谁是你的宝贝儿
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('') 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)