cal*_*iph 2 python protocol-buffers dialogflow-es
我在访问访问 googleDialogflow api 后收到的 protobuf 对象的值时遇到严重问题
(... create google cloud session object ...)
text_input = dialogflow.types.TextInput(text=text, language_code=language_code)
query_input = dialogflow.types.QueryInput(text=text_input)
response = session_client.detect_intent(request={"session": session, "query_input": query_input})
from google.protobuf import json_format
response_json = json_format.MessageToDict(response)
Run Code Online (Sandbox Code Playgroud)
错误是:
AttributeError: 'MapComposite' object has no attribute 'DESCRIPTOR'
基本上我有两个问题:
a)我无法将 protobuf 转换为 json (在这里我可以轻松地看到如何访问我正在寻找的信息)
b) 我无法理解 protobof 数据结构。有字典吗?
a) 或 b) 中的任何帮助者均表示赞赏。
顺便说一句:我正在寻找一种response.parameters在 API 响应中访问的方法。
斯科特的编辑:
response.parameters 是一个对象: <proto.marshal.collections.maps.MapComposite object at 0x7fe7323f7da0>
参数对象如下所述: https: //cloud.google.com/dialogflow/es/docs/reference/rest/v2/DetectIntentResponse#QueryResult
这对你来说有意义吗?因为我不明白如何访问这个对象中的值。
您可以使用MessageToDict如下所示的方法将 protobuf 序列化为字典并使用response._pb而不是response.
from google.protobuf.json_format import MessageToDict
response_json = MessageToDict(response._pb)
Run Code Online (Sandbox Code Playgroud)
现在,您现在可以在字典中导航parameters您想要访问的 ,如下所示,因为parameters位于内部/下方queryResult(基于结构)。
print(response_json["queryResult"]["parameters"])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2987 次 |
| 最近记录: |