Skip to main content
 首页 » 编程设计

python之如何使用python获取json数据的值

2025年05月04日18luoye11

这是我的代码:

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 

但它显示错误:

那我该怎么办呢,

谢谢

请您参考如下方法:

您应该使用 JSON 解析器,例如 simplejson 模块,而不是使用 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'