I am getting a json object from a remote server, and converting it to a python string like this:
a = eval(response)
Is this stupid in any way, or do I have a better option?
I am getting a json object from a remote server, and converting it to a python string like this:
a = eval(response)
Is this stupid in any way, or do I have a better option?
Using eval
is not a good way to process JSON:
JSON isn't even valid Python, because of true
, false
, and null
.
eval
will execute arbitrary Python code, so you are at the mercy of malicious injection of code.
Use the json
module available in the standard library instead:
import json
data = json.loads("[1, 2, 3]")
If you're using a version of Python older than 2.6, you'll need to download the module yourself. It's called simplejson
and can be downloaded from PyPi.
Yes, very. Use a json decoder instead:
>>> from simplejson import loads
>>> loads(response)