Авалония, Foreach запроса API с помощью HttpClient ⇐ C#
-
Anonymous
Авалония, Foreach запроса API с помощью HttpClient
I currently have an API request that I make with Avalonia C# and I manage to return the entire JSON as a result but cannot retrieve each element in a foreach. Here is the current code:
// Get servers infos private async Task GetServersInfos() { using (var httpClient = new HttpClient()) { using (var request = new HttpRequestMessage(new HttpMethod("GET"), "http://example.com/api")) { var response = await httpClient.SendAsync(request); if (response.IsSuccessStatusCode) { string jsonObject = JsonSerializer.Serialize(response.Content); var results = await response.Content.ReadAsStringAsync(); foreach (var result in results) { TestText.Text = result["name"]; } } } } } And the JSON result:
{ "1": { "name": "Example 1", "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit...", }, "2": { "name": "Example 2", "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit...", } } But I have the following error: Unable to apply indexing using [] to an expression of type 'char'.
Источник: https://stackoverflow.com/questions/780 ... httpclient
I currently have an API request that I make with Avalonia C# and I manage to return the entire JSON as a result but cannot retrieve each element in a foreach. Here is the current code:
// Get servers infos private async Task GetServersInfos() { using (var httpClient = new HttpClient()) { using (var request = new HttpRequestMessage(new HttpMethod("GET"), "http://example.com/api")) { var response = await httpClient.SendAsync(request); if (response.IsSuccessStatusCode) { string jsonObject = JsonSerializer.Serialize(response.Content); var results = await response.Content.ReadAsStringAsync(); foreach (var result in results) { TestText.Text = result["name"]; } } } } } And the JSON result:
{ "1": { "name": "Example 1", "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit...", }, "2": { "name": "Example 2", "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit...", } } But I have the following error: Unable to apply indexing using [] to an expression of type 'char'.
Источник: https://stackoverflow.com/questions/780 ... httpclient
Мобильная версия