我想将嵌套字符串转换为列表
list = '[{"dialog_context": {"dialog_active": "orderno","lifespan": 2,"name": "Order_number_dialog_context","parameters": {"orderno": ""},"prompt": "What is the order number?","time_stamp": 1565256993.914126},"followup_context": 'null',"intent": "Order_number","name": "Order_number_context","parameters_context": null,"timestamp": 1565256993.914128}]'
eval(list)
但是我遇到错误
> File "<string>", line 1, in <module> NameError: name 'null' is not
> defined
答案 0 :(得分:0)
鉴于'null'
实际上只是null
,您可以使用json
:
import json
mylist = '[{"dialog_context": {"dialog_active": "orderno","lifespan": 2,"name": "Order_number_dialog_context","parameters": {"orderno": ""},"prompt": "What is the order number?","time_stamp": 1565256993.914126},"followup_context": null,"intent": "Order_number","name": "Order_number_context","parameters_context": null,"timestamp": 1565256993.914128}]'
json.loads(mylist)
[{'dialog_context': {'dialog_active': 'orderno',
'lifespan': 2,
'name': 'Order_number_dialog_context',
'parameters': {'orderno': ''},
'prompt': 'What is the order number?',
'time_stamp': 1565256993.914126},
'followup_context': None,
'intent': 'Order_number',
'name': 'Order_number_context',
'parameters_context': None,
'timestamp': 1565256993.914128}]
如您所见,null
正在转换为None
。