将 JSON 转换为基于另一个属性的对象

我正在编写一些模型并为它们分配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


慕桂英546537
浏览 97回答 2
2回答

慕丝7291255

1)您需要创建一个枚举,其中包含owner_type在 json中具有价值的所有可能成员[JsonConverter(typeof(StringEnumConverter))]public enum EnumOwnerType{&nbsp; &nbsp; [EnumMember(Value = "App\\Models\\User")]&nbsp; &nbsp; User,&nbsp; &nbsp; [EnumMember(Value = "App\\Models\\Group")]&nbsp; &nbsp; Group}您需要添加对项目的引用以进行组装System.Runtime.Serialization,并且在您的程序中您必须导入一些名称空间,例如using System.Runtime.Serialization;对于EnumMemberAttribute。using Newtonsoft.Json;using Newtonsoft.Json.Converters;对于StringEnumConverter。using Newtonsoft.Json.Linq;对于JObject。2)修改你的Vehicle类如下public class Vehicle{&nbsp; &nbsp; [JsonProperty("owner_type")]&nbsp; &nbsp; public EnumOwnerType OwnerType { get; set; }&nbsp; &nbsp; [JsonProperty("owner")]&nbsp; &nbsp; public JObject Owner { get; set; }}在以上类别中的属性OwnerType是类型EnumOwnerType。Owner是类型JObject。3)这是您的示例模型Userclass User{&nbsp; &nbsp; public int id { get; set; }&nbsp; &nbsp; public string&nbsp; username { get; set; }&nbsp; &nbsp; public string email { get; set; }&nbsp; &nbsp; public string email_verified_at { get; set; }&nbsp; &nbsp; public DateTime? created_at { get; set; }&nbsp; &nbsp; public DateTime? updated_at { get; set; }}这是为了Groupclass Group{&nbsp; &nbsp; public int id { get; set; }&nbsp; &nbsp; public string name { get; set; }&nbsp; &nbsp; public DateTime? created_at { get; set; }&nbsp; &nbsp; public DateTime? updated_at { get; set; }&nbsp; &nbsp; public _Color color { get; set; }}class _Color{&nbsp; &nbsp; public int id { get; set; }&nbsp; &nbsp; public string hex { get; set; }&nbsp; &nbsp; public DateTime? created_at { get; set; }&nbsp; &nbsp; public DateTime? updated_at { get; set; }}用法:反序列化代码string json = "Your json here";Vehicle vehicle = JsonConvert.DeserializeObject<Vehicle>(json);switch (vehicle.OwnerType){&nbsp; &nbsp; case EnumOwnerType.User:&nbsp; &nbsp; &nbsp; &nbsp; User user = vehicle.Owner.ToObject<User>();&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case EnumOwnerType.Group:&nbsp; &nbsp; &nbsp; &nbsp; Group group = vehicle.Owner.ToObject<Group>();&nbsp; &nbsp; &nbsp; &nbsp; break;}在上面switch的块中,案例根据OwnerType枚举自动执行,并且Owner是JObject将您的ownerjson 对象类型转换为适当的模型User或Group。输出:(来自调试器)1) 对于你的第一个带有"owner_type": "App\\Models\\User".2) 对于你的第二个 json"owner_type": "App\\Models\\Group"

冉冉说

只是反序列化为动态。您可以使用 ExpandoObject 获得所需内容。var converter = new ExpandoObjectConverter();&nbsp; &nbsp;&nbsp;dynamic customer1 = JsonConvert.DeserializeObject<ExpandoObject>(jsonString, converter);Console.WriteLine(customer1.owner.username);
打开App,查看更多内容
随时随地看视频慕课网APP