在 Chatterbot 和 Django 集成中获取 JSON 的属性值

Get JSON's attribute value in Chatterbot and Django integration

statement.text 在 chatterbot 和 Django 集成中 returns

{'text': u'How are you doing?', 'created_at': datetime.datetime(2017, 2, 20, 7, 37, 30, 746345, tzinfo=<UTC>), 'extra_data': {}, 'in_response_to': [{'text': u'Hi', 'occurrence': 3}]}

我想要一个 text 属性的值,以便打印 How are you doing?

你得到的是字典。字典的值可以通过get()函数获取。您也可以使用 dict['text'],但它不执行错误检查。如果密钥不存在,则获取函数 returns None。

chatterbot return json 对象(dict) 所以你可以像下面那样使用 dictionary 操作

[1]: data = {'text': u'How are you doing?', 'created_at': datetime.datetime(2017, 2, 20, 7, 37, 30, 746345, tzinfo=<UTC>), 'extra_data': {}, 'in_response_to': [{'text': u'Hi', 'occurrence': 3}]}

[2]: data['text'] or data.get('text')[this approch is good].