简体   繁体   中英

What can be used instead of ast module?

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': 'CNDCRNEHLW2XFFCC90PDE77EEJHJF6RC3R8PEVUACB0C34H330.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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM