0
        string imgurID = "";
        string html = string.Empty;
        string url = @"https://api.imgur.com/3/gallery/r/nsfw/top/";

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Headers["Authorization"] = $"Client-ID {imgurID}";
        request.AutomaticDecompression = DecompressionMethods.GZip;

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        using (Stream stream = response.GetResponseStream())
        using (StreamReader reader = new StreamReader(stream))
        {
            html = reader.ReadToEnd();
        }

        try
        {
            ImgurRoot root;
            var json = JsonConvert.DeserializeObject(html);
            root = JsonConvert.DeserializeObject<ImgurRoot>(html);

            ADebug.LogDebug(root.link); //This is Empty

            var index = new Random().Next(0, 100);
            ADebug.LogDebug(json);

        }
        catch(Exception e)
        {
            ADebug.LogCriticalError(e);
        }

In this code i need to get the link from a post from imgur, and those posts are sorted by an index called "data" and i need to get the link for that post, but i don't know how to get just the link for one post and when i tried to get the link for all posts, the link is empty

ImgurRoot:

public class ImgurRoot
{
    public string id { get; set; }
    public string title { get; set; }
    public string description { get; set; }
    public string datetime { get; set; }
    public string type { get; set; }
    public string animated { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string size { get; set; }
    public string views { get; set; }
    public string bandwidth { get; set; }
    public string vote { get; set; }
    public string favorite { get; set; }
    public string nsfw { get; set; }
    public string section { get; set; }
    public string account_url { get; set; }
    public string account_id { get; set; }
    public string is_ad { get; set; }
    public string is_most_viral { get; set; }
    public string has_sound { get; set; }
    public string tags { get; set; }
    public string ad_type { get; set; }
    public string ad_url { get; set; }
    public string in_gallery { get; set; }
    public string link { get; set; }
    public string comment_count { get; set; }
    public string ups { get; set; }
    public string downs { get; set; }
    public string points { get; set; }
    public string score { get; set; }
    public string is_album { get; set; }
}

How i can fix this?

EDIT: The JSON: enter image description here

7
  • Have you checked what the response code on your HttpWebResponse is? That usually provides some information on why you're not getting what you think you should. Commented Sep 6, 2017 at 13:51
  • Could be that the response data you want is deeper in the JSON that you are expecting - can you post the JSON for a response that you've seen come back from the API? Commented Sep 6, 2017 at 13:52
  • I solved, i made the JSON Dynamic and then i selected the data with an index. Commented Sep 6, 2017 at 14:05
  • Please don't add solved to the title. If you wish to self answer click the answer button (I'm not sure if you have enough rep for this). Either way adding solved isn't the right thing to do Commented Sep 6, 2017 at 14:10
  • I'm also not clear how a screen shot solves your issue? Commented Sep 6, 2017 at 14:10

1 Answer 1

1

The fixed code:

        string imgurID = "";
        string html = string.Empty;
        string url = @"https://api.imgur.com/3/gallery/r/nsfw/top/";

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Headers["Authorization"] = $"Client-ID {imgurID}";
        request.AutomaticDecompression = DecompressionMethods.GZip;

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        using (Stream stream = response.GetResponseStream())
        using (StreamReader reader = new StreamReader(stream))
        {
            html = reader.ReadToEnd();
        }

        try
        {
            dynamic json = JsonConvert.DeserializeObject(html);
            var index = new Random().Next(0, 100);
            ADebug.LogDebug(json.data[index].link); //Here is what i edited to fix the problem

        }
        catch(Exception e)
        {
            ADebug.LogCriticalError(e);
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.