使用python从嵌套的json对象中手术提取数据

时间:2016-11-29 21:51:40

标签: python json

从这个嵌套的JSON对象中,我需要解析并打印出"id"的值和"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
                        }
                    },

我一直试图以这种方式抓住它:

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')

但是我无法获取所需的数据。

我怎样才能手术提取我感兴趣的那些数据?

This is the full file

修改

我一直在尝试这种方式作为某种探索,但即使在文件完成之前崩溃:

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 )

1 个答案:

答案 0 :(得分:1)

我正在跳跃并猜测最终你想要的信息,这是每个位置ID的项目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)
相关问题