I have an assignment to create a wpf app that collects data from API website. The data must be displayed in JSON format and I have to show only specific data like, email, first name and so on. I am showing the data in a TextBox and so far I have successfully made that all the API website info is shown. Here is the code so far:
private readonly HttpClient httpClient = new HttpClient();
private const string apiUrl = "https://reqres.in/api/users/3"; // Replace this with the API endpoint URL
public DMCsearch()
{
InitializeComponent();
}
private async void SearchButton_Click(object sender, RoutedEventArgs e)
{
string searchTerm = searchBox.Text.Trim();
if (string.IsNullOrEmpty(searchTerm))
{
MessageBox.Show("Please enter a search term.");
return;
}
try
{
HttpResponseMessage response = await httpClient.GetAsync(apiUrl + "?query=" + searchTerm);
if (response.IsSuccessStatusCode)
{
string jsonResponse = await response.Content.ReadAsStringAsync();
// Here, you can process the jsonResponse as needed and display the data in the resultTextArea.
// For example, you can deserialize the JSON string and display specific information.
resultTextArea.Text = jsonResponse;
}
else
{
MessageBox.Show("Failed to retrieve data. Status code: " + response.StatusCode);
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
}
}
This is the output. As I said I want to display specific values like email
, first_name
, last_name
...
{"data": {"id":3,"email":"emma.wong@reqres.in","first_name":"Emma","last_name":"Wong","avatar":"https://reqres.in/img/faces/3-image.jpg"},"support": {"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}