在python中解析Alexa json响应以获取值名时出现问题

在python中解析Alexa json响应以获取值名时出现问题,json,python-3.x,alexa,alexa-skills-kit,alexa-slot,Json,Python 3.x,Alexa,Alexa Skills Kit,Alexa Slot,我已将以下意图传递给我的处理人: "request": { "type": "IntentRequest", "requestId": "amzn1.echo-api.request.3af5c8c3-1d1f-4169-8ce8-fde1a99a7c8d", "timestamp": "2019-04-03T04:08:06Z", "locale": "en-US", "intent": { "name": "get_speeds",

我已将以下意图传递给我的处理人:

"request": {
    "type": "IntentRequest",
    "requestId": "amzn1.echo-api.request.3af5c8c3-1d1f-4169-8ce8-fde1a99a7c8d",
    "timestamp": "2019-04-03T04:08:06Z",
    "locale": "en-US",
    "intent": {
        "name": "get_speeds",
        "confirmationStatus": "NONE",
        "slots": {
            "direction": {
                "name": "direction",
                "value": "inbound",
                "resolutions": {
                    "resolutionsPerAuthority": [
                        {
                            "authority": "amzn1.er-authority.echo-sdk.amzn1.ask.skill.e76bf13b-71ac-4a90-94d4-597aa597ae87.direction",
                            "status": {
                                "code": "ER_SUCCESS_MATCH"
                            },
                            "values": [
                                {
                                    "value": {
                                        "name": "inbound",
                                        "id": "a8e6fe5b9e68f30a146cefebaa7edcc3"
                                    }
                                }
                            ]
                        }
                    ]
                },
                "confirmationStatus": "NONE",
                "source": "USER"
            }
        }
    },
    "dialogState": "COMPLETED"
}
我想提取实际值,而不是话语,例如值名,在本例中为“inbound”。我已经尝试过这个和各种类似的迭代(打印是为了调试):

我也尝试过使用“resolutionsPerAuthority”的方法,它是传递给我的JSON,但显然不是我程序的结果,正如日志所示:

04:08:07
{'resolutions_per_authority': [{'authority': 'amzn1.er-authority.echo-sdk.amzn1.ask.skill.e76bf13b-71ac-4a90-94d4-597aa597ae87.direction',

04:08:07
'status': {'code': 'ER_SUCCESS_MATCH'},

04:08:07
'values': [{'value': {'id': 'a8e6fe5b9e68f30a146cefebaa7edcc3',

04:08:07
'name': 'inbound'}}]}]}

04:08:07
'Resolutions' object is not subscriptable

这就是我在所有方法中不断遇到的错误:“Resolutions”对象是不可下标的。有人能帮我提取标准槽值吗?我需要为其他几个目的做同样的事情,但我想如果我能让这一个起作用,它将成为其他目的的一个模型

好的,我终于找到了一个python示例,它使用了相对较新的SDK的面向对象版本,就像我一样。该示例是Amazon示例的python版本

基于此,以下工作正在进行:

slots = handler_input.request_envelope.request.intent.slots
direction = slots["direction"].resolutions.resolutions_per_authority[0].values[0].value.name

我仍然希望更好地理解它是如何工作的,但至少它在工作,并且可能会帮助其他人。我发现Alexa的例子和文档有很多,但是它没有很好的组织,api也在不断变化,所以你发现的一些东西已经过时了。

正如你已经指出的,你的问题是你把resolution对象当作是可订阅的,通过

resolutions.resolutions_per_authority[0].values[0].value
这是获得它的正确途径

需要指出的是,在多次匹配的情况下,Alexa将按照最有可能匹配用户意图的顺序返回分辨率

此代码段迭代插槽并返回一个python字典,其中只包含键,以了解它是否经过验证以及匹配值的id:

from ask_sdk_model.slu.entityresolution import StatusCode

@staticmethod
def get_slot_values(filled_slots):
    """Return slot values with additional info."""
    slot_values = {}
    logger.info("Filled slots: {}".format(filled_slots).replace("\n", "\r"))

    for key, slot_item in six.iteritems(filled_slots):
        name = slot_item.name
        try:
            status_code = slot_item.resolutions.resolutions_per_authority[0].status.code

            if status_code == StatusCode.ER_SUCCESS_MATCH:
                slot_values[name] = {
                    "synonym": slot_item.value,
                    "resolved": slot_item.resolutions.resolutions_per_authority[0].values[0].value.__dict__,  # to make it JSON serializable
                    "is_validated": True,
                }
            elif status_code == StatusCode.ER_SUCCESS_NO_MATCH:
                slot_values[name] = {
                    "synonym": slot_item.value,
                    "resolved": slot_item.value,
                    "is_validated": False,
                }
            else:
                pass
        except (AttributeError, ValueError, KeyError, IndexError, TypeError) as e:
            # for BUILT-IN intents, there are no resolutions, but the value is specified
            if slot_item.value is not None and slot_item.value != 'NONE':
                slot_values[name] = {
                    "synonym": slot_item.value,
                    "resolved": slot_item.value,
                    "is_validated": True,
                }
            else:
                logger.info("SLOT {} UNRESOLVED".format(name))
                slot_values[name] = {
                    "synonym": slot_item.value,
                    "resolved": slot_item.value,
                    "is_validated": False,
                }
    return slot_values

其中
filled\u slots=handler\u input.request\u envelope.request.intent.slots

看起来您并没有发布完整的json<代码>'resolutions\u per\u authority'没有出现在您发布的示例词典中。这是令我困惑的事情之一!Alexa测试页面中的技能i/o显示如上所示。有决议权限,但没有决议权限。
from ask_sdk_model.slu.entityresolution import StatusCode

@staticmethod
def get_slot_values(filled_slots):
    """Return slot values with additional info."""
    slot_values = {}
    logger.info("Filled slots: {}".format(filled_slots).replace("\n", "\r"))

    for key, slot_item in six.iteritems(filled_slots):
        name = slot_item.name
        try:
            status_code = slot_item.resolutions.resolutions_per_authority[0].status.code

            if status_code == StatusCode.ER_SUCCESS_MATCH:
                slot_values[name] = {
                    "synonym": slot_item.value,
                    "resolved": slot_item.resolutions.resolutions_per_authority[0].values[0].value.__dict__,  # to make it JSON serializable
                    "is_validated": True,
                }
            elif status_code == StatusCode.ER_SUCCESS_NO_MATCH:
                slot_values[name] = {
                    "synonym": slot_item.value,
                    "resolved": slot_item.value,
                    "is_validated": False,
                }
            else:
                pass
        except (AttributeError, ValueError, KeyError, IndexError, TypeError) as e:
            # for BUILT-IN intents, there are no resolutions, but the value is specified
            if slot_item.value is not None and slot_item.value != 'NONE':
                slot_values[name] = {
                    "synonym": slot_item.value,
                    "resolved": slot_item.value,
                    "is_validated": True,
                }
            else:
                logger.info("SLOT {} UNRESOLVED".format(name))
                slot_values[name] = {
                    "synonym": slot_item.value,
                    "resolved": slot_item.value,
                    "is_validated": False,
                }
    return slot_values