C#。我如何获得随机ID,然后获得较大的URL

C#。我如何获得随机ID,然后获得较大的URL,c#,json,discord,C#,Json,Discord,我尝试了很多东西,但是我的大脑现在不工作了,我不知道该怎么办。我在网上搜索过,但没有找到任何对我有帮助的东西 如果您想要JSON API链接,我尝试使用Hits.ID获取随机搜索结果,并从Hit中获取大图像URL,这样您就可以更好地了解我在尝试做什么 搜索命令 [Command("search")] public async Task Search(CommandContext ctx, string args) { WebClient n

我尝试了很多东西,但是我的大脑现在不工作了,我不知道该怎么办。我在网上搜索过,但没有找到任何对我有帮助的东西


如果您想要JSON API链接,我尝试使用Hits.ID获取随机搜索结果,并从Hit中获取大图像URL,这样您就可以更好地了解我在尝试做什么

搜索命令

[Command("search")]
        public async Task Search(CommandContext ctx, string args)
        {
            WebClient n = new WebClient();

            var json = n.DownloadString("https://pixabay.com/api/?key=###################&q=" + args + "&image_type=photo&pretty=true");
            var root = JsonConvert.DeserializeObject<Root>(json);

            var builder = new DiscordEmbedBuilder
            {
                Color = DiscordColor.Rose,
                Description = "Search Result",
            };

            foreach (Hit hit in root.hits)
            {
                builder.WithImageUrl(hit.largeImageURL);
            }
            await ctx.RespondAsync(embed: builder.Build());
        }
[命令(“搜索”)]
公共异步任务搜索(CommandContext ctx,字符串参数)
{
WebClient n=新的WebClient();
var json=n.DownloadString(“https://pixabay.com/api/?key=###################&q=“+args+”&image_type=photo&pretty=true”);
var root=JsonConvert.DeserializeObject(json);
var builder=新的DiscordEmbedBuilder
{
颜色=不和谐的颜色。玫瑰色,
Description=“搜索结果”,
};
foreach(在根目录中命中。命中)
{
builder.WithImageUrl(hit.largeImageURL);
}
等待ctx.RespondAsync(嵌入:builder.Build());
}
获取设置

        public class Hit
        {
            public string largeImageURL { get; set; }
            public int webformatHeight { get; set; }
            public int webformatWidth { get; set; }
            public int likes { get; set; }
            public int imageWidth { get; set; }
            public int id { get; set; }
            public int user_id { get; set; }
            public int views { get; set; }
            public int comments { get; set; }
            public string pageURL { get; set; }
            public int imageHeight { get; set; }
            public string webformatURL { get; set; }
            public string type { get; set; }
            public int previewHeight { get; set; }
            public string tags { get; set; }
            public int downloads { get; set; }
            public string user { get; set; }
            public int favorites { get; set; }
            public int imageSize { get; set; }
            public int previewWidth { get; set; }
            public string userImageURL { get; set; }
            public string previewURL { get; set; }
        }

        public class Root
        {
            public int totalHits { get; set; }
            public List<Hit> hits { get; set; }
            public int total { get; set; }
        }
公共类命中率
{
公共字符串largeImageURL{get;set;}
public int webformatHeight{get;set;}
public int webformatWidth{get;set;}
公共int类{get;set;}
公共int imageWidth{get;set;}
公共int id{get;set;}
公共int用户_id{get;set;}
公共int视图{get;set;}
公共int注释{get;set;}
公共字符串pageURL{get;set;}
公共int-imageHeight{get;set;}
公共字符串webformatURL{get;set;}
公共字符串类型{get;set;}
公共int预览视图{get;set;}
公共字符串标记{get;set;}
公共int下载{get;set;}
公共字符串用户{get;set;}
公共int收藏夹{get;set;}
公共int imageSize{get;set;}
public int previewWidth{get;set;}
公共字符串userImageURL{get;set;}
公共字符串previewURL{get;set;}
}
公共类根
{
公共整数totalHits{get;set;}
公共列表命中{get;set;}
公共整数总计{get;set;}
}

要从命中列表中获得随机命中,可以使用Linq获得这样的结果

Hit randomHit = root.hits.ElementAt(new Random().Next(x.Count));
var randomUrl = randomHit.largeImageURL;

ElementAt
提供特定索引处的元素。使用该列表中0和总点击数之间的随机数,可以获得随机点击。一旦你有了它,你就可以得到该对象的userImageUrl。

到底是什么问题?我正试图使用Hits.ID获得随机搜索结果,然后从Hit中获得大的图像URL。你可以只使用
root.Hits[new random().Next(x.Count)]
。另外,最好是重复使用random,而不是使用新的。。我认为这是一个更好的解决办法。Op只提到了一次命中,否则为new Random()使用变量是有效的