2017-05-17 63 views
0

反序列化多態性JSON對象我實際嘗試反序列化下一個對象在按照一個單元測試與Patreon與newtonsoft

string json = @"{ 
      ""data"": { 
       ""attributes"": { 
        ""about"": """", 
        ""created"": ""2017-04-01T12:00:00+00:00"", 
        ""discord_id"": null, 
        ""email"": ""[email protected]"", 
        ""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"" 
      } 
      }"; 

但是我的屬性對象或陣列是多態的,它可以是用戶喜歡我爲例,但其他對象類型,我不知道該怎麼做。
我試過類似that但沒有成功。

第二個問題,它是如何平面化具有id和關係的相同用戶類中的屬性的。

編輯: 要anwser我的問題,我發現不是 「複製」 一個更好的解決方案: http://jsonapi.org/

回答

1

不,不是要去工作。

當您序列化一個對象時,Newtonsoft.JSON包有options to include type names,所以反序列化可以與多態性一起正確工作。但使用純JSON,解串器無法知道在陣列中實例化哪個類。