反序列化 json 来自 wikipedia api 的数据以从三个图像信息中获取缩略图

deserialization json data from wikipedia api to get the thumburl from three imageinfo

我在维基百科 api 上请求从每个图像信息中获取 thumburl,这样我就可以在图片框中使用这张图片。我使用 url https://en.wikipedia.org/w/api.php?action=query&prop=imageinfo&format=json&iiprop=url&iiurlwidth=400&titles=File%3ALuftbild%20Flensburg%20Schleswig-Holstein%20Zentrum%20Stadthafen%20Foto%202012%20Wolfgang%20Pehlemann%20Steinberg-Ostsee%20IMG%206187.jpg%7CFile%3AHafen%20St%20Marien%20Flensburg2007.jpg%7CFile%3ANordertor%20im%20Schnee%20(Flensburg%2C%20Januar%202014).JPG 来获取三个图像。我从 json2csharp.

获得了以下 json 文件
  {
    public class Imageinfo
   {
    public string thumburl { get; set; }
    public int thumbwidth { get; set; }
    public int thumbheight { get; set; }
    public string url { get; set; }
    public string descriptionurl { get; set; }
   }

   public class Pageval
   {
    public int ns { get; set; }
    public string title { get; set; }
    public string missing { get; set; }
    public string imagerepository { get; set; }
    public List<Imageinfo> imageinfo { get; set; }
   }


   public class Query
  {
    public Dictionary<string, Pageval> pages { get; set; }
  }

   public class RootObject
   {
    public string batchcomplete { get; set; }
    public Query query { get; set; }
   }




   class Image
   {
    public static PictureBox Image1 = new PictureBox();
    public static PictureBox Image2 = new PictureBox();
    public static PictureBox Image3 = new PictureBox();



    public static void Load_Image1()
    {
        using (var wc = new System.Net.WebClient())
        {
            var uri = ("https://en.wikipedia.org/w/api.php?action=query&prop=imageinfo&format=json&iiprop=url&iiurlwidth=400&titles=File%3ALuftbild%20Flensburg%20Schleswig-Holstein%20Zentrum%20Stadthafen%20Foto%202012%20Wolfgang%20Pehlemann%20Steinberg-Ostsee%20IMG%206187.jpg%7CFile%3AHafen%20St%20Marien%20Flensburg2007.jpg%7CFile%3ANordertor%20im%20Schnee%20(Flensburg%2C%20Januar%202014).JPG");
            var response = wc.DownloadString(new Uri(uri));
            var responseJson = JsonConvert.DeserializeObject<RootObject>(response);

            foreach (KeyValuePair<string, Pageval> entry in responseJson.query.pages)
            {
                var url = entry.Value.imageinfo.First().thumburl;
                for(int i; i<=3 ;i++)
                {

                }

            }

        }
    }
}

我想制作一个 for 循环,通过它我可以在 3 个图片框中显示 3 个图像。但真的不知道该怎么做

首先,您的 class 定义需要一点重构:

  public class Imageinfo
            {
                public string thumburl { get; set; }
                public int thumbwidth { get; set; }
                public int thumbheight { get; set; }
                public string url { get; set; }
                public string descriptionurl { get; set; }
            }

            public class Pageval
            {
                public int ns { get; set; }
                public string title { get; set; }
                public string missing { get; set; }
                public string imagerepository { get; set; }
                public List<Imageinfo> imageinfo { get; set; }
            }


            public class Query
            {
                public Dictionary<string, Pageval> pages { get; set; }
            }

            public class RootObject
            {
                public string batchcomplete { get; set; }
                public Query query { get; set; }
            }


            public class Image
            {
                public static PictureBox Image1 = new PictureBox();
                public static PictureBox Image2 = new PictureBox();
                public static PictureBox Image3 = new PictureBox();             

            }

反序列化逻辑:

   using (var wc = new System.Net.WebClient())
      {
        var uri = ("https://en.wikipedia.org/w/api.php?action=query&prop=imageinfo&format=json&iiprop=url&iiurlwidth=400&titles=File%3ALuftbild%20Flensburg%20Schleswig-Holstein%20Zentrum%20Stadthafen%20Foto%202012%20Wolfgang%20Pehlemann%20Steinberg-Ostsee%20IMG%206187.jpg%7CFile%3AHafen%20St%20Marien%20Flensburg2007.jpg%7CFile%3ANordertor%20im%20Schnee%20(Flensburg%2C%20Januar%202014).JPG");
        var response = wc.DownloadString(new Uri(uri));
        var responseJson = JsonConvert.DeserializeObject<RootObject>(response);

        foreach(KeyValuePair<string, Pageval> entry in responseJson.query.pages)
        {
        var url = entry.Value.imageinfo.First().thumburl;
        //Do something with url
        Console.WriteLine(url);
        }

   }

这给出了网址。请标记!谢谢!