c# - 无法将 JSON 数组用于 Azure 函数

标签 c# json.net azure-functions json-deserialization jsonparser

我的 azure 函数有以下 JSON 输入,但在反序列化对象后无法将其传入

{
  "name": [{
      "SiteName": "Site1",
      "SiteUrl": "https://site1.com/"
    },
    {
      "SiteName": "Site2",
      "SiteUrl": "https://site2.com/"
    },
  ]
}

在反序列化后我得到计数​​为 2 但内部数组值我没有使用下面的代码反序列化

string requestBody = new StreamReader(req.Body).ReadToEnd();
dynamic data = JsonConvert.DeserializeObject(requestBody);
       
var Root = data["name"].ToObject<List<Constant>>();

和如下声明的常量类

class Constant
{
     public Dictionary<string, string> name { get; set; }
} 

最佳答案

尝试创建如下类。

class JsonResponse
{
    public List<Constant> name { get; set; }
}

class Constant
{
    public string SiteName { get; set; }
    public string SiteUrl { get; set; }
}

并尝试使用 JsonConvert.DeserializeObject<JsonResponse>(requestBody) 反序列化响应.

string requestBody = new StreamReader(req.Body).ReadToEnd();
JsonResponse data = JsonConvert.DeserializeObject<JsonResponse>(requestBody);
var Root = data.name;

关于c# - 无法将 JSON 数组用于 Azure 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70936316/

相关文章:

c# File.Delete 恢复?

c# - 一般检查不会在非约束类型上装箱可空值的 null。

c# - MySqlDataReader: DataTable.Fill(reader) 抛出 ConstraintException

c# - JContainer、JObject、JToken和Linq混淆

azure - 停止/终止按消耗计划运行实例的 Azure Functions

azure - 如何使用 ARM 模板将 azure 函数添加到 API 管理

c# - 确定无线网状网络中邻居的 IP 地址

C#:如何添加名为 "protected"的类成员

c# - 在 Newtonsoft JSON.NET 模式中禁用空类型

azure-functions - 如何使用带有 C# 的 Azure Functions Blob 触发器从 Blob 存储中读取 json 文件