来自wikipedia api的反序列化json数据从三个imageinfo获取thumburl

时间:2015-12-12 17:29:46

标签: c# json image deserialization wikipedia-api

我在维基百科api上发出请求,要求从每个图像信息中获取图像,以便我可以在图片框中使用此图片。我使用网址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个图像。但真的不知道该怎么做

1 个答案:

答案 0 :(得分:1)

首先,您的类定义需要一些重构:

    IEnumerator Start()
    {
        yield return new WaitForSeconds(4);
        Application.LoadLevel (1); 
    }

反序列化逻辑:

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

            }

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