You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从Azure Logic App发送JSON请求至Azure Function时数值异常转换

问题:Azure Logic App调用Azure Function时整数被转换为浮点数

问题场景

  • 在Azure Logic App中通过Compose操作构建如下JSON:
{
"status": "Completed",
"result": [
  {
    "id": "some_hash_here",
    "distance": 0,
    "value": 240032
  }
]
}
  • 通过「Call Azure function」块以POST方式发送至C#编写的Azure Function
  • Function通过HttpRequest获取原始请求内容时,发现数字被转为浮点数:
{
"status": "Completed",
"result": [
  {
    "id": "some_hash_here",
    "distance": 0.0,
    "value": 240032.0
  }
]
}
  • Logic App诊断显示「Call Azure function」输入正确,但Function收到的原始内容已被修改;使用Parse JSON块强制架构也无法阻止转换,还引发对象引用错误。

原因分析

核心原因是Azure Logic App内置的JSON序列化机制依赖JavaScript的数字类型规则:

  • JavaScript中没有整数、浮点数的类型区分,所有数字统一为Number类型
  • Logic App在将Compose中的对象序列化为请求内容时,会遵循JS的类型逻辑,把所有数字统一处理为浮点数格式,即使原始是整数
  • Function接收的「原始请求内容」实际是Logic App经过JS序列化后的结果,并非Compose里的原始对象结构

解决方法

方法1:将数字以字符串形式传递

在Compose操作中把需要保留整数类型的字段改为字符串,后续Function再转换为整数:
修改后的Compose JSON:

{
"status": "Completed",
"result": [
  {
    "id": "some_hash_here",
    "distance": "0",
    "value": "240032"
  }
]
}

C# Function中处理逻辑:

var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var jsonObj = JObject.Parse(requestBody);
int distance = int.Parse(jsonObj["result"][0]["distance"].ToString());
int value = int.Parse(jsonObj["result"][0]["value"].ToString());

方法2:在Function中强制转换类型(不修改Logic App)

若无法调整Logic App输出,可在Function中将浮点数强制转为整数(适用于确定数字为整数的场景):

var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var jsonObj = JObject.Parse(requestBody);
int distance = Convert.ToInt32(jsonObj["result"][0]["distance"]);
int value = Convert.ToInt32(jsonObj["result"][0]["value"]);

方法3:用HTTP操作块替代内置调用

绕过Logic App的自动序列化,手动构造HTTP请求:

  1. 保持Compose的原始JSON结构不变
  2. 添加「HTTP」操作块,方法选POST,URL填Function触发地址
  3. 请求头设置Content-Type: application/json
  4. 请求体选择「原始内容」模式,直接引用Compose的输出
    此方式可确保发送的JSON完全保留原始整数格式,避免自动类型转换。

内容的提问来源于stack exchange,提问作者Khushil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 23:35:32