如何将python字符串数据转换成list或dict?

2024-05-23 18:07:44 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我从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"}]}

当我得到这个响应时,我想从dictionary中收集所有的错误键…但是它看起来像是一个字符串,我将尝试使用json中的dumpjson.dumps文件()然后删除斜杠,但对我不起作用,甚至不起作用。我试试这个python json dumps。我错过了什么?请帮我做这个。在


Tags: 代码idjsonidsmessagefailureerrorresults
2条回答

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

#! /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)

由于您接收到的数据是有效的Python数据,您可以简单地使用[ast.literal_评估][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}
>>> 

然后是删除错误

^{pr2}$

相关问题 更多 >