1

this is my code :

a = '''{"title":"sss","body":"wwww:aaa     
<a href='#' onclick='logout()' >fff</a>  
","data":{"status":0,"userName":"www","userId":"433"}}'''
a = eval(a)
print a.title

but it show error :

so what can i do ,

thanks

Saurabh Gokhale
  • 51,864
  • 34
  • 134
  • 163
zjm1126
  • 32,782
  • 53
  • 118
  • 163

4 Answers4

6

You should use a JSON parser such as the simplejson module rather than using eval:

>>> a = '''{"title":"sss",
"body":"wwww:aaa&nbsp;&nbsp;&nbsp;<a href='#' onclick='logout()' >fff</a>",
"data":{"status":0,"userName":"www","userId":"433"}}'''
>>> import simplejson
>>> parsed_data = simplejson.loads(a)
>>> parsed_data['title']
'sss'
Mark Longair
  • 415,589
  • 70
  • 403
  • 320
0

I would also try demjson. this should let you write your json data as a python object and then encode it.

Joseph Le Brech
  • 6,447
  • 11
  • 46
  • 84
0

Works fine here. But you should be using json/simplejson to deserialize, not eval().

Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325
0

Dictionaries are accessed as a["title"] in Python, not a.title.

Apart from this, you should use Python's json module instead of eval().

Sven Marnach
  • 530,615
  • 113
  • 910
  • 808