使用Python解析Json嵌套字典(不是列表中的字典)

时间:2017-07-04 04:03:37

标签: python json dictionary

我有这些可怕的嵌套的JSON词典

  "data": {
            "assetID": "VMSA0000000000310652",
            "lastModified": "2017-06-02T19:36:36.535-04:00",
            "locale": {
              "MetadataAlbum": {
                "Artists": {
                  "Artist": {
                    "ArtistName": "Various Artists",
                    "ArtistRole": "MainArtist"
                  }
                },
                "Publishable": "true",
                "genres": {
                  "genre": {
                    "extraInfos": null,
                  }
                },
                "lastModified": "2017-06-02T19:32:46.296-04:00",
                "locale": {
                  "country": "UK",
                  "language": "en",

并希望能够将语言的值与下面的方法相匹配。我传递语言('en'),数据是上面的嵌套字典。

def get_localized_metadataalbum(language, data):
    for locale in data['locale']:
        if data['locale'].get('MetadataAlbum') is not None:
            if data['locale'].get('MetadataAlbum').get('locale') is not None:
                if data['locale'].get('MetadataAlbum').get('locale').get('language') is not None:
                    if data['locale'].get('MetadataAlbum').get('locale').get('language') == language:
                        return data['locale']

return None

该方法适用于词典列表,但不适用于词典中的词典...任何人都可以指向我可以学习如何通过嵌套词典进行解析的地方吗?我在这里迷失了一些,我发现的所有例子都展示了如何解析字典列表。

我一直在:TypeError: string indices must be integers

2 个答案:

答案 0 :(得分:0)

我修好了你的json。我放入字符串并正确关闭括号。这按预期工作:

import json

json_string = """
  {"data": {
            "assetID": "VMSA0000000000310652",
            "lastModified": "2017-06-02T19:36:36.535-04:00",
            "locale": {
              "MetadataAlbum": {
                "Artists": {
                  "Artist": {
                    "ArtistName": "Various Artists",
                    "ArtistRole": "MainArtist"
                  }
                },
                "Publishable": "true",
                "genres": {
                  "genre": {
                    "extraInfos": null
                  }
                },
                "lastModified": "2017-06-02T19:32:46.296-04:00",
                "locale": {
                  "country": "UK",
                  "language": "en"
                }
              }
           }
         }
   }
   """

json_data = json.loads(json_string)

print(json_data)


def get_localized_metadataalbum(language, data):
    for locale in data['locale']:
        if data['locale'].get('MetadataAlbum') is not None:
            if data['locale'].get('MetadataAlbum').get('locale') is not None:
                if data['locale'].get('MetadataAlbum').get('locale').get('language') is not None:
                    if data['locale'].get('MetadataAlbum').get('locale').get('language') == language:
                        return data['locale']

    return None

print('RESULT:')
print(get_localized_metadataalbum("en", json_data['data']))

我在python 2.7.12上运行它。

答案 1 :(得分:0)

你可能想尝试:除了 像

try:
    assert x in data.keys() for x in ["x","y"]
    ...
    return data["x"]["y"]
except:
    return None