Авалония, Foreach запроса API с помощью HttpClientC#

Место общения программистов C#
Ответить
Гость
 Авалония, 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
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»