开发者

What can be used instead of ast module?

开发者 https://www.devze.com 2023-01-31 10:29 出处:网络
In my project I evaluate a json data to extract information. The code is as follows : conn = httplib.HTTPConnection(host)

In my project I evaluate a json data to extract information. The code is as follows :

conn = httplib.HTTPConnection(host)
conn.request("GET", "/done_json.php")
r = conn.getresponse()
data = r.read()
tmp = ast.literal_eval(data)
list = []
for a in tmp.keys():
    list.append(tmp[a])

How can I change this to be Python 2.4 compatible ?


Sample of my json :

{'key_64': {'size3': 'CNDCRNCDF3FY09XL7UUBCBCPTYE4H7YBG1I5MILNBW172BMHVI.png', 'id': 'CNDCRNCDF3FY09XL7UUBCBCPTYE4H7YBG1I5MILNBW172BMHVI', 'size2': '', 'size1': 'http://dev.geco.com/site/2/CNDCRNCDF3FY09XL7UUBCBCPTYE4H7YBG1I5MILNBW172BMHVI.png'}, 'key_65': {'size3': 'CNDCRNEHLW2XFFCC90PDE开发者_高级运维77EEJHJF6RC3R8PEVUACB0C34H330.png', 'id': 'CNDCRNEHLW2XFFCC90PDE77EEJHJF6RC3R8PEVUACB0C34H330' (...)


Don't use ast.literal_eval, it's the wrong thing for the job anyway. Use simplejson.load(r) (or the standard json module instead of simplejson in newer versions).

ast.literal_eval is designed to evaluate Python literals. JSON is mostly the same, but not entirely (true/false/null instead of True/False/None).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号