【发布时间】:2013-10-11 21:50:11
【问题描述】:
这是我来自 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 中转储然后删除斜线但对我不起作用,甚至 ast 都不起作用。我试试这个python json dumps。我在那里想念什么?请帮助我。
【问题讨论】:
-
如果确实是字符串,使用
json.loads而不是json.dumps。 Dump = 从数据对象到字符串(序列化); load = 从字符串到数据对象(反序列化)。 -
我尝试使用 json.loads...比如在变量中存储响应并在循环中迭代:
coderesult = [ json.loads(resp) for resp in data ]codeand i得到一个错误:codeValueError: 未终止的字符串开始于:第 1 行第 104 列 (char 104)code -
阅读你的问题,尤其是阅读你的 cmets 给的答案,如果你甚至不知道你的输入是字符串还是其他东西,真的会限制任何人帮助你的能力。
-
我知道我在问什么,现在我将我的问题解释到我被卡住的地步......你也可以在代码中显示,输出是字符串并且无法在列表中转换,如果我走错路,请原谅我
标签: python json list dictionary