2017-09-06 6 views
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); 
     } 

に私はimgurからポストからのリンクを取得する必要があり、それらのポストは、「データ」と呼ばれる指標によって並べ替えられています(データが空である)JSONからのデータが動作していない取得し、私はその記事のリンクを取得する必要がありますが、私は1つのポストのためだけのリンクを取得する方法がわからないと私はすべての記事のリンクを取得しようとしたときに、リンクが空であるこのコードでは、HttpWebRequest

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; } 
} 

どうすれば修正できますか?

EDIT:JSON: enter image description here

+0

は、あなたの 'HttpWebResponse'の応答コードが何であるかを確認しましたか?それは、通常、あなたが考えるべきものが得られない理由についての情報を提供します。 – Woody1193

+0

期待しているJSONの中で、必要なレスポンスデータがより深いことがありますか?APIから戻ってきたレスポンスに対してJSONを投稿できますか? – Charleh

+0

私はJSON Dynamicを作ってインデックスを使ってデータを選択しました。 –

答えて

1

固定コード:

 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); 
     }