简体   繁体   中英

Deserialize object in ASP.NET Core 6

Please help me to solve this issue. I am try to call DeserializeObject using JsonConvert , but it's returning an empty result to my view. My API response is fine and receives data, but I cannot deserialize it into a company model.

This is my code:

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

My company model is:

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;
}

The API call is returning a result in JSON format:

{
    "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"
    }
}

The problem is that your returned JSON is an object that has a key of "company" on the root level. Imagine the outer {} as an unnamed object which contains the property "Company", which is an object with its own properties.

Now you have 2 different options:

1:

Create a class that acts as a wrapper around the company to reflect the JSON structure on your .NET objects correctly, eg

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

Then deserialize your JSON result onto this wrapper, it should work now. However this should be avoided in this case because the wrapper itself provides absolutely no other value to your use-case besides, well, acting as a wrapper.

2:

Another way would be to refactor your API call result, if you have access to the API to return the root {} object without the additional, useless "company"-key on the root level. If you don't have access and don't want to touch the JSON result otherwise, then use the wrapper approach.

Also keep in mind that the standard naming convention for classes and properties in C# is Pascal-Case, which means that the first letter of a given name should be uppercase.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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