c# - JSON 反序列化为对象失败

标签 c# .net asp.net-mvc json

return jsSerializer.Deserialize<MamDataResponseHolder>(stringJson);

抛出异常:

Type 'System.String' is not supported for deserialization of an array.

但我没有看到问题。

 public class MamDataResponseHolder
    {
        public MamDataResponsePair[] configuration { get; set; }
        public string Status { get; set; }
    }

  public class MamDataResponsePair
    {
        public string id { get; set; }
        public MamDataResponsecriteria[] criterias { get; set; }
    }

 public class MamDataResponsecriteria
    {
        public Guid criteriaId { get; set; }
        public string[] domains { get; set; }
        public string domainsException { get; set; }
    }

这是 json:

    {
        "configuration": [{
            "id": "Coupon Body",
            "criterias": [{
                "criteriaId": "c7150fc2-72b9-4628-a199-dd5c0bdeef1b",
                "domains": [""],
                "domainsException": [""]
            }]
        }],
        "Status": "succeeded"
    }

最佳答案

您的模型和 Json 不匹配。看一下domainsException。在你的 Json 中它显然是一个字符串数组,但在你的模型中它只是一个字符串。

除此之外:您确定要在 Json 中添加 [""] 吗?这样你就可以得到一个带有空字符串的数组,而不是一个空数组。

关于c# - JSON 反序列化为对象失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15356881/

相关文章:

c# - Xamarin 安卓项目

C# 声明变量时转义字符串标识符

c# - 如何以香港语言获取年份(文化信息 : zh-hk)

c# - 使用 Active Directory 服务帐户从 .NET 连接到 SQL Server

asp.net-mvc - 如何将 MVC 路由添加到特定 Controller ?

c# - 访问嵌套在 .cs 文件下的 .resx 文件的资源包装器时出现 MissingManifestResourceException

c# - 填充但有最小尺寸并使滚动条在 Windows 窗体中工作

两个协变接口(interface)的 C# 协变问题

c# - Razor foreach 循环和 javascript

c# - MVC 是否在 ajax GET 和 POST 操作中以不同方式解析 DateTime 对象?