Last active
May 13, 2020 18:01
-
-
Save einarwh/174221b0fa05aa4e0ab75236782cd522 to your computer and use it in GitHub Desktop.
Anonymous types can actually be used as targets for deserialization.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var paidCartJsonString = @"{ | |
""_state"": ""paid"", | |
""paidItems"": [ | |
{ | |
""id"": ""1bcd"", | |
""title"": ""gizmo"" | |
}, | |
{ | |
""id"" : ""3cdf"", | |
""title"": ""widget"", | |
""description"": ""A very useful item"" | |
} | |
], | |
""payment"": { | |
""amount"": 123.5, | |
""currency"": ""USD"" | |
}, | |
""timestamp"": ""2020-04-11T10:11:33.514+02:00"" | |
}"; | |
var anonymousPaidCartObject = JsonConvert.DeserializeAnonymousType(paidCartJsonString, | |
new | |
{ | |
_state = default(string), | |
paidItems = new [] { | |
new { | |
id = default(string), | |
title = default(string), | |
description = default(string) | |
} | |
}, | |
payment = new | |
{ | |
amount = default(double), | |
currency = default(string) | |
}, | |
timestamp = default(string) | |
}); | |
var firstItemTitle = anonymousPaidCartObject.paidItems[0].title; | |
var currency = anonymousPaidCartObject.payment.currency; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment