我实际上尝试按照Patreon
反序列化单元测试中的下一个对象string json = @"{
""data"": {
""attributes"": {
""about"": """",
""created"": ""2017-04-01T12:00:00+00:00"",
""discord_id"": null,
""email"": ""james@mymail.com"",
""facebook"": null,
""facebook_id"": null,
""first_name"": ""Firstname"",
""full_name"": ""Fullname"",
""gender"": 0,
""has_password"": true,
""image_url"": ""https://c3.patreon.com/2/patreon-user/abc.jpg"",
""is_deleted"": false,
""is_email_verified"": true,
""is_nuked"": false,
""is_suspended"": false,
""last_name"": """",
""social_connections"": {
""deviantart"": null,
""discord"": null,
""facebook"": null,
""spotify"": null,
""twitch"": null,
""twitter"": null,
""youtube"": null
},
""thumb_url"": ""https://c3.patreon.com/2/patreon-user/abc.jpg"",
""twitch"": null,
""twitter"": null,
""url"": ""https://www.patreon.com/user?u=123456"",
""vanity"": null,
""youtube"": null
},
""id"": ""123456"",
""relationships"": {
""pledges"": {
""data"": []
}
},
""type"": ""user""
},
""links"": {
""self"": ""https://api.patreon.com/user/123456""
}
}";
但是我的属性对象或数组是多态的,它可以是User,就像我的例子,但是其他对象类型,我不知道该怎么办。
我试过像that这样的东西但没有成功。
第二个问题,如何在具有id和关系的同一用户类中平面化属性。
编辑: 为了回答我的问题,我找到了比“复制”更好的解决方案: http://jsonapi.org/
答案 0 :(得分:2)
不,不会工作。
序列化对象时,Newtonsoft.JSON包具有options to include type names,因此反序列化可以正确地使用多态。但是使用普通的JSON,反序列化器无法知道在数组中实例化哪个类。