检查密钥是否存在,并使用Python迭代JSON数组


130

我从Facebook帖子中获得了一堆JSON数据,如下所示:

{"from": {"id": "8", "name": "Mary Pinter"}, "message": "How ARE you?", "comments": {"count": 0}, "updated_time": "2012-05-01", "created_time": "2012-05-01", "to": {"data": [{"id": "1543", "name": "Honey Pinter"}]}, "type": "status", "id": "id_7"}

JSON数据是半结构化的,并且所有数据都不相同。下面是我的代码:

import json 

str = '{"from": {"id": "8", "name": "Mary Pinter"}, "message": "How ARE you?", "comments": {"count": 0}, "updated_time": "2012-05-01", "created_time": "2012-05-01", "to": {"data": [{"id": "1543", "name": "Honey Pinter"}]}, "type": "status", "id": "id_7"}'
data = json.loads(str)

post_id = data['id']
post_type = data['type']
print(post_id)
print(post_type)

created_time = data['created_time']
updated_time = data['updated_time']
print(created_time)
print(updated_time)

if data.get('application'):
    app_id = data['application'].get('id', 0)
    print(app_id)
else:
    print('null')

#if data.get('to'):
#... This is the part I am not sure how to do
# Since it is in the form "to": {"data":[{"id":...}]}

我希望代码将to_id打印为1543,否则打印'null'

我不确定该怎么做。

Answers:


162
import json

jsonData = """{"from": {"id": "8", "name": "Mary Pinter"}, "message": "How ARE you?", "comments": {"count": 0}, "updated_time": "2012-05-01", "created_time": "2012-05-01", "to": {"data": [{"id": "1543", "name": "Honey Pinter"}]}, "type": "status", "id": "id_7"}"""

def getTargetIds(jsonData):
    data = json.loads(jsonData)
    if 'to' not in data:
        raise ValueError("No target in given data")
    if 'data' not in data['to']:
        raise ValueError("No data for target")

    for dest in data['to']['data']:
        if 'id' not in dest:
            continue
        targetId = dest['id']
        print("to_id:", targetId)

输出:

In [9]: getTargetIds(s)
to_id: 1543

6
为什么要进行这种显式in检查,以及raise是否缺少这些检查?只需访问它而不进行检查,您将获得完全相同的行为(除了用KeyError而不是代替ValueError)。
abarnert 2014年

100

如果您只想检查密钥是否存在

h = {'a': 1}
'b' in h # returns False

如果要检查是否有密钥值

h.get('b') # returns None

如果缺少实际值,则返回默认值

h.get('b', 'Default value')

将返回“空”,而不是作为在的情况下,预期对于b“默认值” {“一个”:1,“B”:空}
米克尔

16

为此类事件创建助手实用程序方法是一个好习惯,这样,每当您需要更改属性验证的逻辑时,它就会放在一个位置,并且对于跟随者而言,代码将更具可读性。

例如,在以下位置创建一个辅助方法(或JsonUtils带有静态方法的类)json_utils.py

def get_attribute(data, attribute, default_value):
    return data.get(attribute) or default_value

然后在您的项目中使用它:

from json_utils import get_attribute

def my_cool_iteration_func(data):

    data_to = get_attribute(data, 'to', None)
    if not data_to:
        return

    data_to_data = get_attribute(data_to, 'data', [])
    for item in data_to_data:
        print('The id is: %s' % get_attribute(item, 'id', 'null'))

重要的提示:

我使用的原因data.get(attribute) or default_value不是简单的data.get(attribute, default_value)

{'my_key': None}.get('my_key', 'nothing') # returns None
{'my_key': None}.get('my_key') or 'nothing' # returns 'nothing'

在我的应用程序中,获取属性值为“ null”与根本不获取属性相同。如果您的用法不同,则需要进行更改。


4
jsonData = """{"from": {"id": "8", "name": "Mary Pinter"}, "message": "How ARE you?", "comments": {"count": 0}, "updated_time": "2012-05-01", "created_time": "2012-05-01", "to": {"data": [{"id": "1543", "name": "Honey Pinter"}, {"name": "Joe Schmoe"}]}, "type": "status", "id": "id_7"}"""

def getTargetIds(jsonData):
    data = json.loads(jsonData)
    for dest in data['to']['data']:
        print("to_id:", dest.get('id', 'null'))

试试吧:

>>> getTargetIds(jsonData)
to_id: 1543
to_id: null

或者,如果您只想跳过缺少ID的值,而不是打印'null'

def getTargetIds(jsonData):
    data = json.loads(jsonData)
    for dest in data['to']['data']:
        if 'id' in to_id:
            print("to_id:", dest['id'])

所以:

>>> getTargetIds(jsonData)
to_id: 1543

当然,在现实生活中,您可能不想使用print每个id,而是要存储它们并对其进行操作,但这是另一个问题。



4

为此,我编写了一个小函数。随时调整用途,

def is_json_key_present(json, key):
    try:
        buf = json[key]
    except KeyError:
        return False

    return True
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.