JsonSerializer.DeserializeAsync<T> 未反序列化
JsonSerializer.DeserializeAsync<T> Not deserializing
我正在使用我的第一个 Blazor 应用,运行 遇到了一个奇怪的问题。我有一个 Web API returns 一个相当基本的 JSON 响应:
{
"entityId": 26,
"notifications": [
{
"type": "Success",
"message": "The operation completed successfully."
}
],
"hasErrors": false,
"hasWarnings": false
}
有一个符合上述属性的标准 POCO class。在 Blazor 应用程序中,当我尝试从 Http.PutAsJsonAsync<T>
获取响应时,会创建一个 POCO class 的实例(因此它不为空并且不会引发错误),但是 none 以上值实际存在。 EntityId
属性 为空, Notifications
已实例化但为空。我尝试访问响应的方式是:
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,所以它真的很困惑为什么它会创建一个 class 的新实例,但不是映射任何值。
有什么想法吗?
**** 编辑 - 包括 POCO 定义 ****
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 在上面提供了这个答案。
看来您需要添加 JsonAttribute 来管理区分大小写。
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};
这是我的完整解决方案:
var retList = new List<PocoSomeClassData> ();
var response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode) {
using (var reponseStream = await response.Content.ReadAsStreamAsync())
{
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};
retList = await JsonSerializer.DeserializeAsync < List<PocoSomeClassData> > (reponseStream,options);
}
}
我正在使用我的第一个 Blazor 应用,运行 遇到了一个奇怪的问题。我有一个 Web API returns 一个相当基本的 JSON 响应:
{
"entityId": 26,
"notifications": [
{
"type": "Success",
"message": "The operation completed successfully."
}
],
"hasErrors": false,
"hasWarnings": false
}
有一个符合上述属性的标准 POCO class。在 Blazor 应用程序中,当我尝试从 Http.PutAsJsonAsync<T>
获取响应时,会创建一个 POCO class 的实例(因此它不为空并且不会引发错误),但是 none 以上值实际存在。 EntityId
属性 为空, Notifications
已实例化但为空。我尝试访问响应的方式是:
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,所以它真的很困惑为什么它会创建一个 class 的新实例,但不是映射任何值。
有什么想法吗?
**** 编辑 - 包括 POCO 定义 ****
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 在上面提供了这个答案。
看来您需要添加 JsonAttribute 来管理区分大小写。
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};
这是我的完整解决方案:
var retList = new List<PocoSomeClassData> ();
var response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode) {
using (var reponseStream = await response.Content.ReadAsStreamAsync())
{
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};
retList = await JsonSerializer.DeserializeAsync < List<PocoSomeClassData> > (reponseStream,options);
}
}