Вывод json с сервера
Здравствуйте. как из кода ниже заполнить listbox данными из json
1234567891011121314151617
public JObject GetCountryList()
{
string url = $"{_baseUrl}getCountryList";
using (var client = new HttpClient())
{
var response = client.GetAsync(url).Result;
if (response.IsSuccessStatusCode)
{
var json = JObject.Parse(response.Content.ReadAsStringAsync().Result);
return json;
}
else
{
throw new Exception(response.StatusCode.ToString());
}
}
}
По дате
По рейтингу
примерно так
123456789101112131415161718
public void PopulateListBox(ListBox listBox)
{
try
{
JObject json = GetCountryList();
JArray countries = (JArray)json["countries"];
listBox.Items.Clear();
foreach (var country in countries)
{
listBox.Items.Add(country["name"].ToString());
}
}
catch (Exception ex)
{
MessageBox.Show($"Error fetching country list: {ex.Message}");
}
}