Iva*_*bla 8 c# json jsonserializer asp.net-web-api system.text.json
我有一个返回一些 Json 的 webapi:
{"id":9,"businessName":"dummy","address":"pluto","products":[
{"id":762,"description":"Centralized needs-based website","price":1281.24,"stock":1600,"categories":[],"factory":null},
{"id":1027,"description":"Realigned 6th generation knowledge base","price":2398.16,"stock":19583,"categories":[],"factory":null},
{"id":1392,"description":"User-centric zero administration array","price":998.07,"stock":6124,"categories":[],"factory":null},
{"id":1800,"description":"Team-oriented reciprocal core","price":4422.95,"stock":17372,"categories":[],"factory":null},
{"id":2763,"description":"Sharable needs-based hierarchy","price":4122.98,"stock":17397,"categories":[],"factory":null},
{"id":6189,"description":"Re-engineered hybrid emulation","price":395.09,"stock":532,"categories":[],"factory":null}
]}
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用以下方法反序列化:
using var response = await _httpClient.GetAsync($"{GetApiRouteFromEntity(entity)}/{entity.GetId()}");
response.EnsureSuccessStatusCode();
var responseContent = await response.Content.ReadAsStringAsync();
T? item = JsonSerializer.Deserialize<T>(responseContent);
Run Code Online (Sandbox Code Playgroud)
但这给了我一个空工厂,id为0,所有其他属性为null
工厂.cs
public class Factory : EntityBase
{
[DisplayName("ID")]
public int Id { get; set; }
[DisplayName("Nome Business")]
public string? BusinessName { get; set; }
[DisplayName("Indirizzo")]
public string? Address { get; set; }
[DisplayName("Prodotti")]
public virtual ICollection<Product> Products { get; set; }
public override string ToString()
{
return $"[{Id}] {BusinessName}";
}
}
Run Code Online (Sandbox Code Playgroud)
产品.cs
public class Product : EntityBase
{
[DisplayName("ID")]
public int Id { get; set; }
[DisplayName("Descrizione")]
public string? Description { get; set; }
[DisplayName("Prezzo")]
public float Price { get; set; }
[DisplayName("Magazzino")]
public int Stock { get; set; }
[DisplayName("Categorie")]
public virtual ICollection<Category> Categories { get; set; }
[DisplayName("Fabbrica")]
public virtual Factory Factory { get; set; }
public override string ToString()
{
return $"[{Id}] {Description}";
}
}
Run Code Online (Sandbox Code Playgroud)
EntityBase.cs
public abstract class EntityBase
{
public virtual object GetId()
{
return GetType().GetProperty("Id").GetValue(this);
}
}
Run Code Online (Sandbox Code Playgroud)
我猜这是因为产品中的工厂道具为空,但我不知道如何解决这个问题
khl*_*hlr 13
JSON 键都是驼峰式大小写,但类中的 props 是用帕斯卡式大小写编写的。例如businessName对BusinessName.. 因此 JsonSerializer 无法匹配它们。
您可以像这样初始化序列化器以忽略大小写的差异:
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
T? item = JsonSerializer.Deserialize<T>(responseContent, options);
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅文档:https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-character-casing
来自文档:
默认情况下,属性名称和字典键在 JSON 输出中保持不变,包括大小写。
您可以指定属性命名策略:
T? item = JsonSerializer.Deserialize<T>(responseContent, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
Run Code Online (Sandbox Code Playgroud)
对所有 JSON 属性名称使用驼峰式大小写,或使用包含JsonPropertyNameAttibute正确名称的方式标记所有需要的属性:
public class Factory : EntityBase
{
[DisplayName("ID")]
[JsonPropertyName("id")] // and so on
public int Id { get; set; }
....
}
Run Code Online (Sandbox Code Playgroud)
或者设置JsonSerializerOptions.PropertyNameCaseInsensitive为true.