从json获取值列表

时间:2015-11-29 16:19:14

标签: python json

我正在使用网站上的API,我正在努力从我收到的JSON中收集所需的数据。以下是我收到的JSON示例:

{'success': True, 'tracks': {'data': {'12958571': {'position': 3, 'id': '12958571llwU', 'track': 'Are You With Me', 'bitrate': '320', 'text_id': None, 'count_listen': '2110', 'artist': 'Lost Frequencies feat Easton Corbin', 'server_id4': '0', 'lenght': '211'}, '12924079': {'position': 8, 'id': '12924079tZmc', 'track': 'Love Me Like You Do', 'bitrate': '320', 'text_id': None, 'count_listen': '1654', 'artist': 'Ellie Goulding', 'server_id4': '0', 'lenght': '233'}, '12241830': {'position': 12, 'id': '12241830KIyP', 'track': 'Blame', 'bitrate': 'VBR', 'text_id': None, 'count_listen': '1093', 'artist': 'Calvin Harris feat. John Newman', 'server_id4': 's20', 'lenght': '211'}, '1036820': {'position': 16, 'id': '1036820770C', 'track': 'LILY WAS HERE', 'bitrate': '256', 'text_id': None, 'count_listen': '307', 'artist': 'CANDY DULFER  DAVE STEWART', 'server_id4': 's20', 'lenght': '253'}, '12441354': {'position': 14, 'id': '12441354CAj2', 'track': 'Love Who Loves You Back', 'bitrate': 'VBR', 'text_id': None, 'count_listen': '975', 'artist': 'Tokio Hotel', 'server_id4': '0', 'lenght': '229'}, '12697344': {'position': 13, 'id': '12697344Polh', 'track': 'Outside', 'bitrate': '160', 'text_id': None, 'count_listen': '1050', 'artist': 'Calvin Harris feat. Ellie Goulding', 'server_id4': '0', 'lenght': '226'}, '5738480': {'position': 5, 'id': '5738480NApX', 'track': 'Love Me Again', 'bitrate': '320', 'text_id': '18660', 'count_listen': '1830', 'artist': 'John Newman', 'server_id4': '0', 'lenght': '215'}, '12538432': {'position': 4, 'id': '12538432tnyz', 'track': 'Dangerous', 'bitrate': 'VBR', 'text_id': None, 'count_listen': '1916', 'artist': 'David Guetta (feat. Sam Martin)', 'server_id4': '0', 'lenght': '169'}, '12881624': {'position': 9, 'id': '12881624Igo4', 'track': 'The Good, The Bad And The Crazy', 'bitrate': '320', 'text_id': None, 'count_listen': '1374', 'artist': 'Imany, Filatov, Karas', 'server_id4': '0', 'lenght': '260'}, '6449584': {'position': 19, 'id': '6449584PwRx', 'track': 'Wrecking Ball', 'bitrate': '192', 'text_id': '18512', 'count_listen': '259', 'artist': 'Miley Cyrus', 'server_id4': '0', 'lenght': '223'}, '13361656': {'position': 11, 'id': '13361656Gr1A', 'track': 'Reality', 'bitrate': 'VBR', 'text_id': None, 'count_listen': '1131', 'artist': 'Lost Frequencies feat. Janieck Devy', 'server_id4': '0', 'lenght': '158'}, '6810440': {'position': 17, 'id': '6810440Uc4B', 'track': 'Royals', 'bitrate': '320', 'text_id': None, 'count_listen': '272', 'artist': 'Lorde', 'server_id4': '0', 'lenght': '192'}, '13241397': {'position': 7, 'id': '13241397v1h3', 'track': 'Ghost Town', 'bitrate': 'VBR', 'text_id': None, 'count_listen': '1668', 'artist': 'Adam Lambert', 'server_id4': '0', 'lenght': '208'}, '13482545': {'position': 1, 'id': '13482545fiqs', 'track': 'Supergirl', 'bitrate': '320', 'text_id': None, 'count_listen': '2431', 'artist': 'Anna Naklab, Younotus feat. Alle Farben', 'server_id4': '0', 'lenght': '212'}, '13154936': {'position': 2, 'id': '131549364jlY', 'track': 'Lean On', 'bitrate': 'VBR', 'text_id': None, 'count_listen': '2297', 'artist': 'Major Lazer x DJ Snake feat. MØ', 'server_id4': '0', 'lenght': '176'}, '13416218': {'position': 15, 'id': '134162185MCV', 'track': 'How Deep Is Your Love', 'bitrate': '320', 'text_id': None, 'count_listen': '922', 'artist': 'Calvin Harris & Disciples', 'server_id4': '0', 'lenght': '212'}, '8021072': {'position': 20, 'id': '8021072mjSx', 'track': 'Under Control', 'bitrate': '320', 'text_id': None, 'count_listen': '256', 'artist': 'Alesso feat. Calvin Harris & Hurts', 'server_id4': '0', 'lenght': '215'}, '12496862': {'position': 10, 'id': '124968620fP6', 'track': 'Imagination', 'bitrate': '320', 'text_id': None, 'count_listen': '1295', 'artist': 'Gorgon City feat. Katy Menditta', 'server_id4': '0', 'lenght': '387'}, '12701434': {'position': 6, 'id': '12701434zPSR', 'track': 'Tribute', 'bitrate': 'VBR', 'text_id': None, 'count_listen': '1801', 'artist': 'Homeless Man', 'server_id4': '0', 'lenght': '94'}, '13478793': {'position': 18, 'id': '13478793fu3c', 'track': 'Hello', 'bitrate': '320', 'text_id': None, 'count_listen': '268', 'artist': 'Adele', 'server_id4': '0', 'lenght': '295'}}, 'count': 1000}}

很抱歉,JSON全部在一行中。无论如何,由于某种原因,这里的筑巢是完全灾难性的。我想要的是为每个表示的轨道设置一个数组。轨道嵌套在数据键中,但每个轨道不在数组中(如api所声称的那样),但实际上是字典的一部分,其中键是其id(即'12958571')。然而,如果键的值会根据我从api得到的响应而变化,我很难看到如何收集我需要的数据(艺术家,曲目等)。有没有办法可以获取所有数据并将其转换为数组,即使我不知道密钥的值是什么?

编辑:这是来自服务器的纯有效JSON:

{
"success": true,
"tracks": {
    "count": 1000,
    "data": {
        "13482545": {
            "id": "13482545fiqs",
            "artist": "Anna Naklab, Younotus feat. Alle Farben",
            "track": "Supergirl",
            "lenght": "212",
            "text_id": null,
            "bitrate": "320",
            "server_id4": "0",
            "count_listen": "2466",
            "position": 1
        },
        "13154936": {
            "id": "131549364jlY",
            "artist": "Major Lazer x DJ Snake feat. M\u00d8",
            "track": "Lean On",
            "lenght": "176",
            "text_id": null,
            "bitrate": "VBR",
            "server_id4": "0",
            "count_listen": "2335",
            "position": 2
        },
        "12958571": {
            "id": "12958571llwU",
            "artist": "Lost Frequencies feat Easton Corbin",
            "track": "Are You With Me",
            "lenght": "211",
            "text_id": null,
            "bitrate": "320",
            "server_id4": "0",
            "count_listen": "2140",
            "position": 3
        },
        "12538432": {
            "id": "12538432tnyz",
            "artist": "David Guetta (feat. Sam Martin)",
            "track": "Dangerous",
            "lenght": "169",
            "text_id": null,
            "bitrate": "VBR",
            "server_id4": "0",
            "count_listen": "1950",
            "position": 4
        },
        "5738480": {
            "id": "5738480NApX",
            "artist": "John Newman",
            "track": "Love Me Again",
            "lenght": "215",
            "text_id": "18660",
            "bitrate": "320",
            "server_id4": "0",
            "count_listen": "1860",
            "position": 5
        },
        "12701434": {
            "id": "12701434zPSR",
            "artist": "Homeless Man",
            "track": "Tribute",
            "lenght": "94",
            "text_id": null,
            "bitrate": "VBR",
            "server_id4": "0",
            "count_listen": "1834",
            "position": 6
        },
        "13241397": {
            "id": "13241397v1h3",
            "artist": "Adam Lambert",
            "track": "Ghost Town",
            "lenght": "208",
            "text_id": null,
            "bitrate": "VBR",
            "server_id4": "0",
            "count_listen": "1696",
            "position": 7
        },
        "12924079": {
            "id": "12924079tZmc",
            "artist": "Ellie Goulding",
            "track": "Love Me Like You Do",
            "lenght": "233",
            "text_id": null,
            "bitrate": "320",
            "server_id4": "0",
            "count_listen": "1680",
            "position": 8
        },
        "12881624": {
            "id": "12881624Igo4",
            "artist": "Imany, Filatov, Karas",
            "track": "The Good, The Bad And The Crazy",
            "lenght": "260",
            "text_id": null,
            "bitrate": "320",
            "server_id4": "0",
            "count_listen": "1402",
            "position": 9
        },
        "12496862": {
            "id": "124968620fP6",
            "artist": "Gorgon City feat. Katy Menditta",
            "track": "Imagination",
            "lenght": "387",
            "text_id": null,
            "bitrate": "320",
            "server_id4": "0",
            "count_listen": "1317",
            "position": 10
        },
        "13361656": {
            "id": "13361656Gr1A",
            "artist": "Lost Frequencies feat. Janieck Devy",
            "track": "Reality",
            "lenght": "158",
            "text_id": null,
            "bitrate": "VBR",
            "server_id4": "0",
            "count_listen": "1146",
            "position": 11
        },
        "12241830": {
            "id": "12241830KIyP",
            "artist": "Calvin Harris feat. John Newman",
            "track": "Blame",
            "lenght": "211",
            "text_id": null,
            "bitrate": "VBR",
            "server_id4": "s20",
            "count_listen": "1112",
            "position": 12
        },
        "12697344": {
            "id": "12697344Polh",
            "artist": "Calvin Harris feat. Ellie Goulding",
            "track": "Outside",
            "lenght": "226",
            "text_id": null,
            "bitrate": "160",
            "server_id4": "0",
            "count_listen": "1071",
            "position": 13
        },
        "12441354": {
            "id": "12441354CAj2",
            "artist": "Tokio Hotel",
            "track": "Love Who Loves You Back",
            "lenght": "229",
            "text_id": null,
            "bitrate": "VBR",
            "server_id4": "0",
            "count_listen": "990",
            "position": 14
        },
        "13416218": {
            "id": "134162185MCV",
            "artist": "Calvin Harris & Disciples",
            "track": "How Deep Is Your Love",
            "lenght": "212",
            "text_id": null,
            "bitrate": "320",
            "server_id4": "0",
            "count_listen": "930",
            "position": 15
        },
        "1036820": {
            "id": "1036820770C",
            "artist": "CANDY DULFER  DAVE STEWART",
            "track": "LILY WAS HERE",
            "lenght": "253",
            "text_id": null,
            "bitrate": "256",
            "server_id4": "s20",
            "count_listen": "318",
            "position": 16
        },
        "6810440": {
            "id": "6810440Uc4B",
            "artist": "Lorde",
            "track": "Royals",
            "lenght": "192",
            "text_id": null,
            "bitrate": "320",
            "server_id4": "0",
            "count_listen": "275",
            "position": 17
        },
        "13478793": {
            "id": "13478793fu3c",
            "artist": "Adele",
            "track": "Hello",
            "lenght": "295",
            "text_id": null,
            "bitrate": "320",
            "server_id4": "0",
            "count_listen": "272",
            "position": 18
        },
        "6449584": {
            "id": "6449584PwRx",
            "artist": "Miley Cyrus",
            "track": "Wrecking Ball",
            "lenght": "223",
            "text_id": "18512",
            "bitrate": "192",
            "server_id4": "0",
            "count_listen": "267",
            "position": 19
        },
        "4473614": {
            "id": "44736143Ee9",
            "artist": "Eminem feat. Rihanna",
            "track": "Love The Way You Lie",
            "lenght": "263",
            "text_id": "18072",
            "bitrate": "192",
            "server_id4": "0",
            "count_listen": "260",
            "position": 20
        }
    }
}}

2 个答案:

答案 0 :(得分:2)

假设您获取的JSON对象名为jobject,那么这里是关于如何访问信息的建议:

print('Success?', jobject['success'])
print('Number of tracks claimed:', jobject['tracks']['count'])
print('Number of actual tracks:', len(jobject['tracks']['data']))

for track_id, track_info in jobject['tracks']['data'].items():
    print('Track:', track_info['track'])
    print('Artist:', track_info['artist'])
    print('---')

输出:

Success? True
Number of tracks claimed: 1000
Number of actual tracks: 20
Track: Royals
Artist: Lorde
---
Track: Dangerous
Artist: David Guetta (feat. Sam Martin)
---
Track: Love Who Loves You Back
Artist: Tokio Hotel
---
...

答案 1 :(得分:0)

首先检查您的JSON以确保其有效,即使用http://jsonlint.com

# coding=utf-8
#!/usr/local/bin/python
import json

jsonString = "{'success': True, ..." #your JSON string here

jdict = json.decoder.JSONDecoder().decode(jsonString)

然后jdict将包含一个表示JSON所有内容的字典,您可以使用它来访问。

tracks = jdict['tracks']

您可能需要相应地手动构建和重新排列这些子/词典。您可以轻松地在python中合并两个词典,例如

z = x.copy()
z.update(y)