Created
April 28, 2022 22:43
-
-
Save caisback/42ac86aa48989ecfd79a89ff47860a71 to your computer and use it in GitHub Desktop.
C# - Calling API with Deserialize to Class
This file contains hidden or 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 cultureInfo = new CultureInfo("en-US"); | |
| // // cultureInfo.NumberFormat.CurrencySymbol = "P"; | |
| // CultureInfo.DefaultThreadCurrentCulture = cultureInfo; | |
| // CultureInfo.DefaultThreadCurrentUICulture = cultureInfo; | |
| const string REST_API = "https://api.domain.com/v1/markets"; | |
| //List<Market> markets; | |
| HttpResponseMessage response = await httpClient.GetAsync(REST_API); | |
| if (response.IsSuccessStatusCode) | |
| { | |
| // markets = await response.Content.ReadAsStringAsync() <Market>(); | |
| Console.WriteLine("Request Message Information:- \n\n" + response.RequestMessage + "\n"); | |
| Console.WriteLine("Response Message Header \n\n" + response.Content.Headers + "\n"); | |
| // Get the response | |
| var marketsJsonString = await response.Content.ReadAsStringAsync(); | |
| Console.WriteLine("Your response data is: " + marketsJsonString.Length); | |
| Root deserialized = JsonSerializer.Deserialize<Root>(marketsJsonString); | |
| Console.WriteLine("Your deserialized response data is: " + deserialized); | |
| foreach (var item in deserialized.markets) | |
| { | |
| Console.WriteLine("{0}, ask: {1}", item.symbol, item.ask); | |
| } | |
| var uniqueSymbols = deserialized.markets.DistinctBy(p => p.product).OrderBy(p => p.product); | |
| foreach (var item in uniqueSymbols) | |
| { | |
| Console.WriteLine("{0}, ask: {1}", item.product, item.ask); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment