JsonSerializer.DeserializeAsync< T>不反序列化 [英] JsonSerializer.DeserializeAsync<T> Not deserializing

查看:357
本文介绍了JsonSerializer.DeserializeAsync< T>不反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用第一个Blazor应用程序,但遇到了一个奇怪的问题.我有一个返回基本的JSON响应的Web API:

I am working with my first Blazor app and running into a strange problem. I have a Web API that returns a fairly basic JSON response:

{
    "entityId": 26,
    "notifications": [
        {
            "type": "Success",
            "message": "The operation completed successfully."
        }
    ],
    "hasErrors": false,
    "hasWarnings": false
}

有一个与上述属性匹配的标准POCO类.从Blazor应用程序中,当我尝试从 Http.PutAsJsonAsync< T> 获取响应时,将创建POCO类的实例(因此它不为null且不会引发错误),但实际上没有上述任何值. EntityId 属性为null,而 Notifications 实例化但为空.我尝试访问响应的方式是:

There is a standard POCO class that matches the above properties. From the Blazor app, when I try to get the response from an Http.PutAsJsonAsync<T>, an instance of the POCO class is created (so it's not null and doesn't throw an error), but none of the values above are actually present. The EntityId property is null and Notifications is instantiated but empty. The way I'm trying to access the response is:

var result = await Http.PutAsJsonAsync<ManufacturerModel>($"manufacturers", (ManufacturerModel)context.Model);
if (result.IsSuccessStatusCode)
{
  var response = await JsonSerializer.DeserializeAsync<EntityResponseModel>(result.Content.ReadAsStream());
    
  //response isn't null - it's just a newly created object with nothing mapped
}

通过Chrome中的控制台,我已经确认返回了正确的JSON,因此,为什么要创建该类的新实例,却不映射任何值,这确实令人困惑.

Via the console in Chrome, I've confirmed the proper JSON is returned so it's really confusing why it'd create a new instance of that class, but not map any of the values.

有什么想法吗?

****编辑-包括POCO定义****

**** EDIT - including POCO definition ****

public class EntityResponseModel : BaseModel
{
    /// <summary>
    /// Gets or sets the Id of the affected entity
    /// </summary>
    public long? EntityId { get; set; }
}

public class BaseModel
{
    public BaseModel()
    {
        this.Notifications = new EntityNotificationCollection();
    }

    #region Validation

    /// <summary>
    /// Adds a notification to this model.
    /// </summary>
    /// <param name="type">The type of notification</param>
    /// <param name="message">The message to display</param>
    public void AddNotification(EntityNotificationTypes type, string message)
    {
        this.Notifications.Add(new EntityNotification { Type = type, Message = message });
    }

    /// <summary>
    /// Gets or sets the collection of notifications
    /// </summary>
    public EntityNotificationCollection Notifications { get; private set; }

    /// <summary>
    /// Gets whether errors exist on this model.
    /// </summary>
    //[JsonIgnore]
    public bool HasErrors { get => this.Notifications.HasErrors; }

    /// <summary>
    /// Gets whether warnings exist on this model
    /// </summary>
    //[JsonIgnore]
    public bool HasWarnings { get => this.Notifications.HasWarnings; }

    #endregion
}

推荐答案

您可以指定序列化设置并将其定义为区分大小写或不区分大小写.CharlieFace在上面提供了此答案.

You can specify your serialization settings and define it as being case sensitive or insensitive. CharlieFace provided this answer above.

看起来您需要添加JsonAttribute来管理区分大小写.

Looks like you need to add your JsonAttribute to managing your case sensitivity.

var options = new JsonSerializerOptions
{
    PropertyNameCaseInsensitive = true,
};

这篇关于JsonSerializer.DeserializeAsync&lt; T&gt;不反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆