HttpClient client = new HttpClient();
string URL = "https://api.dictionaryapi.dev/api/v2/entries/en/";
Console.WriteLine("Enter a word: ");
string Input = Console.ReadLine();
string response = await client.GetStringAsync(URL Input);
List<GetSet> gs = JsonConvert.DeserializeObject<List<GetSet>>(response);
Console.ReadKey();
public class GetSet
{
public string word {get; set;}
public string origin {get; set;}
public string definition {get; set;}
public string partOfSpeech {get; set;}
}
I don't want to print out everything in the URL, only the strings that are present in GetSet class. I can't figure it out for the life of me. any help is greatly appreciated
CodePudding user response:
By using Console.WriteLine
along with the string you want to print. Since you're using a list, you have to seperately handle each object in that list.
Consider this piece of code.
foreach(var getset in gs)
{
Console.WriteLine($"Word: {getset.word}");
}
We iterate through the collection of GetSet objects that you created using JsonConvert.DeserializeObject<List<GetSet>>(response);
and use string interpolation (the $""
syntax) to create a string. We then pass that string to WriteLine, which outputs that string as a line of text in the console.
Some varations on the above, illustrating some different ways of creating and passing strings:
Console.WriteLine("someString");
Console.WriteLine(someVariable "someString");
Console.WriteLine($"{someVariable} someString")