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

3
  • 6
    Sorry, can't help. Never seen this error before. Commented Mar 28, 2011 at 12:20
  • possible duplicate of Noob: How to decode JSON with Python Commented Mar 28, 2011 at 12:23
  • 3
    you've asked nearly 1000 questions here, you should know by now: you need to show the error you got. Commented Mar 28, 2011 at 12:25

4 Answers 4

5

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'
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

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().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.