コード例 #1
0
        public async Task <Person> GetPersonByName(string name)
        {
            try
            {
                var client   = GetClient();
                var response = await client.GetAsync($"people/?search={name}");

                string personJSON = await response.Content.ReadAsStringAsync();

                PersonRootobject peopleRoot = JsonSerializer.Deserialize <PersonRootobject>(personJSON);
                List <Person>    people     = peopleRoot.results.ToList <Person>();
                return(people[0]);
            }
            catch
            {
                return(null);
            }
        }
コード例 #2
0
        public async Task <List <Person> > GetPeople()
        {
            var client = GetClient();

            client.BaseAddress = null;
            List <Person> people  = new List <Person>();
            string        nextUri = "https://swapi.dev/api/people/";

            do
            {
                Uri uri      = new Uri(nextUri);
                var response = await client.GetAsync(uri);

                string personJSON = await response.Content.ReadAsStringAsync();

                PersonRootobject peopleRoot = JsonSerializer.Deserialize <PersonRootobject>(personJSON);
                people.AddRange(peopleRoot.results.ToList <Person>());
                nextUri = peopleRoot.next;
            } while (nextUri != null && nextUri != "");
            return(people);
        }