我有以下字符串:
"[{\"id\":360030281574,\"value\":\"http://www.supercupstockcarseries.com/\"},{\"id\":360027795053,\"value\":\"account\"}]"
我需要把这个转换成一个词。 所以我把它加载到json.loads中。
例如,
res=json.loads(the_above_str)
它给出的类型为列表。 我有以下功能将列表转换为dict:
def conv(res):
... res_dict={res[i]:res[i+1] for i in range(0, len(res),2)}
... return res_dict
获取以下错误:
TypeError:不可卸载类型:“dict”
如有任何帮助,我们将不胜感激。
我的预期结果如下:
{"id":"360030281574", "value":"http://www.supercupstockcarseries.com"},
{"id":"360027795053", "value":"account"}
您不需要将List转换为Dict,因为您提供的JSON字符串实际上是一个包含许多项的列表,这些项是您需要的字典,因此您所需要的只是迭代该列表:
res={i:json.loads(text)[i] for i in range(len(json.loads(text))) }
使用它,您将在变量res中有一个字典,其中键为数字,并且值是您上面的值。 结果将是:
{0: {'id': 360030281574, 'value': 'http://www.supercupstockcarseries.com/'}, 1: {'id': 360027795053, 'value': 'account'}}