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
  • 53,625
  • 36
  • 139
  • 164
zjm1126
  • 34,604
  • 53
  • 121
  • 166

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
  • 446,582
  • 72
  • 411
  • 327
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,541
  • 11
  • 49
  • 84
0

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

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
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
  • 574,206
  • 118
  • 941
  • 841