我正在编写一些模型并为它们分配JsonProperty属性,以便我可以使用Newtonsoft.Json. 我的问题是我的 Laravel 后端返回多态关系,所以这意味着我的属性可以是动态类型。
由用户拥有:
{
"id": 1,
"owner_id": 1,
"owner_type": "App\\Models\\User",
"created_at": "2019-04-21 08:57:53",
"updated_at": "2019-04-21 08:57:53",
"owner": {
"id": 1,
"username": "testuser",
"email": "test123@mail.com",
"email_verified_at": null,
"created_at": "2019-04-20 10:23:50",
"updated_at": "2019-04-20 10:23:50"
}
}
由集团拥有:
{
"id": 1,
"owner_id": 1,
"owner_type": "App\\Models\\Group",
"created_at": "2019-04-21 08:57:53",
"updated_at": "2019-04-21 08:57:53",
"owner": {
"id": 1,
"name": "Administration",
"created_at": "2019-04-01 00:00:00",
"updated_at": "2019-04-01 00:00:00",
"color": {
"id": 52,
"hex": "#426D42",
"created_at": null,
"updated_at": null
}
}
}
我正在尝试将 映射owner到我的 C# 项目中的适当模型类。主要型号是Vehicle:
using Newtonsoft.Json;
namespace OmegaAPI.Models
{
public class Vehicle
{
[JsonProperty("owner_type")]
private string OwnerType { get; set; }
[JsonProperty("owner")]
public object Owner { get; set; }
}
}
例如,我有User和Group模型类,它们可以是Vehicle. 如何使用该属性转换owner为适当的模型类?OwnerType
慕丝7291255
冉冉说
相关分类