在 Azure http 触发器 post 方法中计算动态 json 数据时发生 500 内部服务器错误
500 Internal server error while counting dynamic json data in Azure http trigger post method
我正在创建一个 Azure 函数 http 触发器 post 方法,我在 body 中接收 json 数据,然后映射它并将其存储在 blob storage.It 在本地运行良好,但在 Azure 环境中它获得 failed.In 应用程序洞察力 我创建了一个自定义日志事件,其中我看到错误是由于计算 json 动态数据造成的。
error:Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
无法对空引用执行运行时绑定
这是我的示例代码
public 异步任务 运行([ HttpTrigger( AuthorizationLevel.Anonymous, "get", "post", Route = null) ] HttpRequest req )
{
try
{
StringBuilder csveventcontent = new StringBuilder();
string requestBody =
await new StreamReader(req.Body).ReadToEndAsync();
dynamic json = JsonConvert.DeserializeObject(requestBody);
int eventsDataloopCount = json["body"]["data"].Count;
示例 http 请求数据
{
"函数名": "abc",
“方法”:“POST”,
“headers”:{},
“body”:{
“数据”: [
{
“项目1”:1,
"created_at": "2021-02-10T21:07:08Z",
“项目 2”:“b”,
“项目 3”:-1,
“item4”:空,
“item5”:空,
“数组项”:{
“项目1”:“1”,
“项目2”:“2”,
“项目 3”:“3”
},
“数据”: {},
“数组事件”:{
“abc”:1
}
},
我发现您的 JSON 中有两个问题。
- 应该是
"item2": "b"
而不是"item2": b
。
"item1"
有两个键值 "1"
和 "3"
。所以你必须删除其中一个。
所以我的建议是先修复 JSON 然后再继续。
知道问题了,您应该使用 json["body"]["data"].Count
而不是 json["body"]["data"].count
。 Newtonsoft.Json.Linq.JArray
没有计数方法。
我正在创建一个 Azure 函数 http 触发器 post 方法,我在 body 中接收 json 数据,然后映射它并将其存储在 blob storage.It 在本地运行良好,但在 Azure 环境中它获得 failed.In 应用程序洞察力 我创建了一个自定义日志事件,其中我看到错误是由于计算 json 动态数据造成的。 error:Microsoft.CSharp.RuntimeBinder.RuntimeBinderException 无法对空引用执行运行时绑定 这是我的示例代码 public 异步任务 运行([ HttpTrigger( AuthorizationLevel.Anonymous, "get", "post", Route = null) ] HttpRequest req ) {
try
{
StringBuilder csveventcontent = new StringBuilder();
string requestBody =
await new StreamReader(req.Body).ReadToEndAsync();
dynamic json = JsonConvert.DeserializeObject(requestBody);
int eventsDataloopCount = json["body"]["data"].Count;
示例 http 请求数据 { "函数名": "abc", “方法”:“POST”, “headers”:{}, “body”:{ “数据”: [ { “项目1”:1, "created_at": "2021-02-10T21:07:08Z", “项目 2”:“b”, “项目 3”:-1, “item4”:空, “item5”:空, “数组项”:{ “项目1”:“1”, “项目2”:“2”, “项目 3”:“3” }, “数据”: {}, “数组事件”:{ “abc”:1 } },
我发现您的 JSON 中有两个问题。
- 应该是
"item2": "b"
而不是"item2": b
。 "item1"
有两个键值"1"
和"3"
。所以你必须删除其中一个。
所以我的建议是先修复 JSON 然后再继续。
知道问题了,您应该使用 json["body"]["data"].Count
而不是 json["body"]["data"].count
。 Newtonsoft.Json.Linq.JArray
没有计数方法。