繁体   English   中英

在 ASP.NET Core 6 中反序列化 object

[英]Deserialize object in ASP.NET Core 6

请帮我解决这个问题。 我尝试使用JsonConvert调用DeserializeObject ,但它向我的视图返回了一个空结果。 我的 API 响应很好并接收数据,但我无法将其反序列化为公司 model。

这是我的代码:

if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
    string apiResponse = await response.Content.ReadAsStringAsync();
    company = JsonConvert.DeserializeObject<company>(apiResponse);
}

我公司model是:

public class company
{
    public string bankAccount { get; set; } = string.Empty;
    public string bankName { get; set; } = string.Empty;
    public string cif { get; set; } = string.Empty;

    public string creationDate { get; set; } = string.Empty;
    public string email { get; set; } = string.Empty;
    public string fiscalAttribute { get; set; } = string.Empty;
    public string id { get; set; } = string.Empty;

    public string locationAddress { get; set; } = string.Empty;
    public string locationCounty { get; set; } = string.Empty;
    public string name { get; set; } = string.Empty;
    public string nrRegCom { get; set; } = string.Empty;

    public string passwordHash { get; set; } = string.Empty;
    public string phoneNumber { get; set; } = string.Empty;
    public string privilegeKey { get; set; } = string.Empty;

    public string registrationState { get; set; } = string.Empty;
    public string storedHardwareID { get; set; } = string.Empty;

    public string subscriptionExpirationDate { get; set; } = string.Empty;
    public string subscriptionID { get; set; } = string.Empty;
    public string actionConfirmationID { get; set; } = string.Empty;
    public string updateDate { get; set; } = string.Empty;
}

API 调用以 JSON 格式返回结果:

{
    "company": {
        "bankAccount": "777",
        "bankName": "777",
        "cif": "123",
        "creationDate": "2023-01-16T17:38:45.962Z",
        "email": "pankaj@gmail.com",
        "fiscalAttribute": "123",
        "id": "f0e3bfef5bcf40c987713153e55dceef",
        "locationAddress": "india",
        "locationCounty": "india",
        "name": "pankaj",
        "nrRegCom": "123",
        "passwordHash": "7c78eea7a591b0c8a4dad680372e35ca12e11cffdac5c69a39700c8014fbcc82",
        "phoneNumber": "88888858",
        "privilegeKey": "",
        "registrationState": 1,
        "storedHardwareID": "",
        "subscriptionExpirationDate": "0001-01-01T00:00:00Z",
        "subscriptionID": "",
        "actionConfirmationID": "",
        "updateDate": "2023-01-16T17:38:45.962Z"
    }
}

问题是您返回的 JSON 是一个 object,它在根级别上具有“公司”键。 将外部 {} 想象成一个未命名的 object,其中包含属性“Company”,这是一个具有自己属性的 object。

现在你有两个不同的选择:

1:

创建一个 class 作为公司的包装器,以正确反映 .NET 对象上的 JSON 结构,例如

public class CompanyWrapper
{
    public Company company { get; set; }
}

然后将你的 JSON 结果反序列化到这个包装器上,它现在应该可以工作了。 然而,在这种情况下应该避免这种情况,因为包装器本身绝对没有为您的用例提供任何其他价值,除了充当包装器之外。

2:

另一种方法是重构您的 API 调用结果,如果您有权访问 API 以返回根 {} object 而没有额外的,无用的“公司”键在根级别。 如果您无权访问并且不想以其他方式触摸 JSON 结果,请使用包装器方法。

还要记住,C# 中类和属性的标准命名约定是 Pascal-Case,这意味着给定名称的第一个字母应该是大写的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM