这是针对python 3.x,我正在努力转换以下内容
['{"from": "USD", "to": "EUR"}']
到
[{"from": "USD", "to": "EUR"}]
我尝试过使用ast.literal_eval(s),但是用单引号返回我的json,我已经尝试了不同的组合超过一个小时无济于事,非常感谢任何帮助!
答案 0 :(得分:3)
使用json.dumps
和import json
a = ['{"from": "USD", "to": "EUR"}']
obj = json.loads(a[0])
print(obj, type(obj))
>> {'to': 'EUR', 'from': 'USD'} <class 'dict'>
print(json.dumps(obj), type(json.dumps(obj)))
>> {"to": "EUR", "from": "USD"} <class 'str'>
。它会照顾你:
alert cpu.is.too.high {
template = test
$metric = q("sum:rate{counter,,1}:os.cpu{host=your-system-here}", "1h", "")
$avgcpu = avg($metric)
crit = $avgcpu > 80
warn = $avgcpu > 60
}
答案 1 :(得分:0)
>>> c=['{"from": "USD", "to": "EUR"}']
>>> c[0]
'{"from": "USD", "to": "EUR"}'
>>> eval( c[0])
{'to': 'EUR', 'from': 'USD'}
>>> p=eval(c[0])
>>> print p
{'to': 'EUR', 'from': 'USD'}
>>> type(p)
<type 'dict'>