2

这是我来自 GCM-python 的响应代码,

{"multicast_id":6343554431392278573,"success":5,"failure":15,"canonical_ids":0,"results":[{"message_id":"0:1380910865603840%356b
9054f9fd7ecd"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":
"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"message_id":"0:1380910865592683%356b9054f9fd7ecd"},{"erro
r":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"er
ror":"NotRegistered"},{"message_id":"0:1380910865600910%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"message_id":"0:1380910865
596592%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"message_id":"0:1380910865595499%356b9054f9fd7ecd"}]}

当我收到此响应时,我想从字典中收集所有错误键...但它似乎是一个字符串,我将尝试使用json.dumps()转储到 json 中,然后删除斜杠但对我不起作用,甚至没有工作。我试试这个python json dumps。我在那里想念什么?请帮助我。

4

2 回答 2

4

如果它是一个字符串,加载它,不要转储它:

#! /usr/bin/python3

import json

a = '''{"multicast_id":6343554431392278573,"success":5,"failure":15,"canonical_ids":0,"results":[{"message_id":"0:1380910865603840%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":
"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"message_id":"0:1380910865592683%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"message_id":"0:1380910865600910%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"message_id":"0:1380910865596592%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"message_id":"0:1380910865595499%356b9054f9fd7ecd"}]}'''

j = json.loads(a)
errors = [d for d in j ['results'] if 'error' in d]
print(errors)
于 2013-10-04T19:15:44.787 回答
2

由于您收到的数据是有效的 Python 数据,您可以简单地使用 [ast.literal_eval][1]

演示

import ast
data = '''{"multicast_id":6343554431392278573,"success":5,"failure":15,"canonical_ids":0,"results":[{"message_id":"0:1380910865603840%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":
"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"message_id":"0:1380910865592683%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"message_id":"0:1380910865600910%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"message_id":"0:1380910865596592%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"message_id":"0:1380910865595499%356b9054f9fd7ecd"}]}'''

>>> pp.pprint(ast.literal_eval(data))
{   'canonical_ids': 0,
    'failure': 15,
    'multicast_id': 6343554431392278573L,
    'results': [   {   'message_id': '0:1380910865603840%356b9054f9fd7ecd'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'message_id': '0:1380910865592683%356b9054f9fd7ecd'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'message_id': '0:1380910865600910%356b9054f9fd7ecd'},
                   {   'error': 'NotRegistered'},
                   {   'message_id': '0:1380910865596592%356b9054f9fd7ecd'},
                   {   'error': 'NotRegistered'},
                   {   'message_id': '0:1380910865595499%356b9054f9fd7ecd'}],
    'success': 5}
>>> 

其次是转储错误

>>> pp.pprint([elem['error'] for elem in ast.literal_eval(data)['results'] if 'error' in elem])
[   'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered']
于 2013-10-04T19:25:50.133 回答