C# 从JSON获取数据在HttpWebRequest中不起作用(数据为空)
我怎样才能解决这个问题 编辑:JSON: 固定代码:C# 从JSON获取数据在HttpWebRequest中不起作用(数据为空),c#,json,visual-studio,C#,Json,Visual Studio,我怎样才能解决这个问题 编辑:JSON: 固定代码: string imgurID = ""; string html = string.Empty; string url = @"https://api.imgur.com/3/gallery/r/nsfw/top/"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request
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);
}
您是否检查了HttpWebResponse上的响应代码?这通常会提供一些信息,说明为什么您没有得到您认为应该得到的信息。可能是您想要的响应数据在您期望的JSON中更深-您可以发布JSON以获得您从API返回的响应吗?我解决了,我将JSON设置为动态,然后选择了带有索引的数据。请不要在标题中添加已解决的问题。如果您希望自行回答,请单击“回答”按钮,我不确定您是否有足够的代表。无论哪种方式,添加已解决的问题都不是正确的做法我也不清楚屏幕截图如何解决您的问题?
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; }
}
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);
}