简体   繁体   中英

How to retrieve a string value from a URL API using Json (C#)?

Hello Stackoverflow Masters!

I am trying to retrieve a random Chuck Norris joke from the API using JSON, but I am not sure how to retrieve the string value for value on the URL. This is what I was using, but I feel lost! Thank you for your kind help!

using (var client = new HttpClient())
                {
                    joke = client.GetStringAsync(@"https://api.chucknorris.io/jokes/random").Result;
                    JObject result = JObject.Parse(joke);
                    foreach (var results in result)
                    {
                        string value = (string)result["value"];
                    } 
                    
                }
                MessageBox.Show(value);

There are some issues with the code provided (regarding best practices). The following would be better to call the API and get the response.

You need to create a class to deserialize the json into:

public class ChuckNorrisJoke {
    public string Icon_Url { get; set; }
    public string Id { get; set; }
    public string Url { get; set; }
    public string Value { get; set; }
}

Then you can send the request and deserialize the response:

private static HttpClient httpClient = new HttpClient();

var uri = new Uri("https://api.chucknorris.io/jokes/random");
var reqMessage = new HttpRequestMessage(HttpMethod.Get, uri);

var response = httpClient.SendAsync(reqMessage);
var responseContent = await response.Content.ReadAsStringAsync();
var chuckNorrisJoke = JsonConvert.DeserializeObject<ChuckNorrisJoke>(responseContent);

This code will call the endpoint you provided with a GET request. Get the response, and read the content returned from the response, then turn the json in the response into the ChuckNorrisJoke object.

After that, you can access the joke by using:

var joke = chuckNorrisJoke.Value;

Frist create a POCO class like:

public class Random
{
    public string[] categories { get; set; }
    public DateTime created_at { get; set; }
    public string icon_url { get; set; }
    public string Id { get; set; }
    public DateTime updated_at { get; set; }
    public string url { get; set; }
    public string value { get; set; }
}

then deserialize using Newtonsoft.Json nutget pack, you can add static function inside a class, this return a Random object type:

public static Random deserialize(string res)
{
    return JsonConvert.DeserializeObject<Random>(res);
}

for using:

        using (var client = new HttpClient())
        {
            var joke = client.GetStringAsync(@"https://api.chucknorris.io/jokes/random").Result;


            var returnedObject = Random.deserialize(joke);
            string value = returnedObject.value;

            // Console.WriteLine(value);
            MessageBox.Show(value);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM