您好,我为 Azure 函数创建了一个 golang 自定义处理程序。我有一个队列,其中包含需要出列和处理的消息。但是,当我尝试处理该消息时,我无法解码其内容。我正在按照这个例子 https://learn.microsoft.com/en-us/azure/azure-functions/functions-custom-handlers#examples当然,我将其调整为与 gingonic 一起使用,但我仍然无法解决问题。出列的消息不应该是 JSON 格式并且很容易反序列化/解码吗?似乎是某种我不知道如何使用 golang 处理的转义字符串。欢迎任何帮助。 下面:function.json、代码片段以及负载外观和错误的日志消息。
{
"bindings": [
{
"name": "myitem",
"type": "serviceBusTrigger",
"direction": "in",
"queueName": "new-mwsgs",
"connection": "my_connectionstr",
"accessRights": "listen",
"dataType": "string"
}
]
}
type InvokeRequest struct {
Data map[string]json.RawMessage
Metadata map[string]interface{}
}
type MyItem strutc{
ID string `json:"id"`
IDnum int `json:"id_num"`
Flags map[string]interface{} `json:"flags"`
}
func Post(ctx *gin.Context) {
var req InvokeRequest
if err := ctx.BindJSON(&req); err != nil {
logrus.Error(err)
}
logrus.Info(req.Data["myitem"])
var myItem MyItem
if err:= json.Unmarshal([invokeRequest.Data["myitem"], &myItem); err !=nil{
logrus.Error(err)
}
ctx.Status(http.StatusOK)
}
日志
2021-03-26T15:59:04Z [Error] time="2021-03-26T15:59:03Z" level=info msg="map[myitem:\"{\\\"id\\\":\\\"605e04c7fd8f28e17f747952\\\",\\\"id_num\\\":666,\\\"flags\\\":{\\\"eligible\\\":true,\\\"nationwide\\\":true,\\\"persistent_\\\":false}}\"]\""
2021-03-26T15:59:04Z [Error] time="2021-03-26T15:59:03Z" level=error msg="json: cannot unmarshal string into Go value of type MyItem"
最佳答案
这就是我修复它的方法。我在解码之前添加了一个 strconv.Unquote
。现在可以工作了。
myItemStr, _:= strconv.Unquote(invokeRequest.Data["myitem"])
var myItem MyItem
if err:= json.Unmarshal([]byte(myItemStr), &myItem); err !=nil{
logrus.Error(err)
}
关于azure - ServiceBus Dequeue 触发消息在 azure 中解码 Golang 自定义处理程序 : message format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66822465/