我有一个字典列表,我从我的javascript文件发送到我的视图。 现在,当我打印列表时,其类型显示为unicode。我想找回原来的名单。 我怎么能这样做?
我的清单:
[{"employee_id":13,"admin":false,"approver":false,"emp_code_or_email":"second@emp.com,120","manager":false,"emp_name":"second emp","department":"IT","position":"IT","new_emp":false,"manager_and_approver":false,"role_type":"employee","s_no":1},{"employee_id":144,"admin":false,"approver":false,"emp_code_or_email":"107anku@gmail.com,1","manager":true,"emp_name":"A1 A2","department":"IT","position":"Developer","new_emp":false,"manager_and_approver":false,"role_type":"manager","s_no":2}]
我只是想从中获取employee_ids。
我尝试了eval(),list(),encode(' utf-8')。但无济于事。
答案 0 :(得分:1)
您拥有的是JSON的字符串表示形式。我可以通过查看"admin":false
告诉您,以便获得employee_id
。首先需要将列表中的每个json表示转换为字典。
>>> import json
>>> data_raw = '''[{"employee_id":13,"admin":false,"approver":false,"emp_code_or_email":"second@emp.com,120","manager":false,"emp_name":"second emp","department":"IT","position":"IT","new_emp":false,"manager_and_approver":false,"role_type":"employee","s_no":1},{"employee_id":144,"admin":false,"approver":false,"emp_code_or_email":"107anku@gmail.com,1","manager":true,"emp_name":"A1 A2","department":"IT","position":"Developer","new_emp":false,"manager_and_approver":false,"role_type":"manager","s_no":2}]'''
>>> data_json = json.loads(data_raw)
>>> [data['employee_id'] for data in data_json]
[13, 144]
>>>
答案 1 :(得分:0)
当你打印任何对象时,python将显示该对象的python表示。
要按照您想要的方式显示,您应该格式化输出。
但最简单的方法是将它转换为json,因为你从ajax接收它。
import simplejson
simplejson.dumps(data)