访问网址:http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1
根据上面地址的结构,我暂时研究到就三项属性有效,他们分别是
1、format,非必要。我理解为输出格式,不存在或者不等于js,即为xml格式,等于js时,输出json格式;
2、idx,非必要。不存在或者等于0时,输出当天的图片,-1为已经预备用于明天显示的信息,1则为昨天的图片,idx最多获取到前16天的图片信息;*
3、n,必要。这是输出信息的数量,比如n=1,即为1条,以此类推,至多输出8条;*
*号注释:此处我们要注意的时,是否正常的输出信息,与n和idx有关,通过idx的值,我们就可以获得之前bing所使用的背景图片的信息了。
转载自: http://oss.so/blog/55.html
使用C#获取必应每日图片,并解析Json格式结果。
第一步:使用HttpWebRequest/HttpWebResponse获取网页内容。
using System.Net; string BingPictureRestfulURLTomorrow = @"http://cn.bing.com/HPImageArchive.aspx?format=js&idx=-1&n=1"; string jsonContent = string.Empty; HttpWebRequest request = HttpWebRequest.Create(new Uri(BingPictureRestfulURLTomorrow)) as HttpWebRequest; using (HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse) { StreamReader reader = new StreamReader(response.GetResponseStream()); jsonContent = reader.ReadToEnd(); }
第二步:分享返回的网页内容。
{"images":[{"startdate":"20150701","fullstartdate":"201507011600","enddate":"20150702","url":"/az/hprichbg/rb/YalaNPPeafowl_ZH-CN7079851094_1920x1080.jpg","urlbase":"/az/hprichbg/rb/YalaNPPeafowl_ZH-CN7079851094","copyright":"斯里兰卡,雅拉国家公园的蓝孔雀 (© Kevin Schafer/Minden Pictures)","copyrightlink":"http://www.bing.com/search?q=%E8%93%9D%E5%AD%94%E9%9B%80&form=hpcapt&mkt=zh-cn","wp":true,"hsh":"8955f4d08658120f8e709b961a3521ab","drk":1,"top":1,"bot":1,"hs":[{"desc":"凤凰?","link":"http://www.bing.com/search?q=%E8%93%9D%E5%AD%94%E9%9B%80&FORM=hphot1&mkt=zh-cn","query":"不,它是……","locx":13,"locy":34},{"desc":"它仪态万千,","link":"http://www.bing.com/images/search?q=%E8%93%9D%E5%AD%94%E9%9B%80&FORM=hphot2&mkt=zh-cn","query":"快去瞧瞧吧。","locx":39,"locy":46},{"desc":"孔雀是印度的国鸟,","link":"http://www.bing.com/search?q=%E5%AD%94%E9%9B%80%E7%8E%8B%E6%9C%9D+%E5%8D%B0%E5%BA%A6&form=hphot3&mkt=zh-cn","query":"甚至有一个王朝都叫……","locx":63,"locy":40}],"msg":[{"title":"今日图片故事","link":"http://www.bing.com/search?q=%E8%93%9D%E5%AD%94%E9%9B%80&form=pgbar1&mkt=zh-cn","text":"神圣的国王"}]}],"tooltips":{"loading":"正在加载...","previous":"上一页","next":"下一页","walle":"此图片不能下载用作壁纸。","walls":"下载今日美图。仅限用作桌面壁纸。"}}
第三步:定义反序列化的类型。
using System.Collections.Generic; using Newtonsoft.Json; namespace BingPicToday.Model { public class BingPicture { [JsonProperty(PropertyName = "images")] public List<Img> Images; [JsonProperty(PropertyName = "tooltips")] public ToolTip ToolTips; } public class Img { [JsonProperty(PropertyName = "startdate")] public string StartDate; [JsonProperty(PropertyName = "fullstartdate")] public string FullStartDate; [JsonProperty(PropertyName = "enddate")] public string EndDate; [JsonProperty(PropertyName = "url")] public string URL; [JsonProperty(PropertyName = "urlbase")] public string UrlBase; [JsonProperty(PropertyName = "copyright")] public string CopyRight; [JsonProperty(PropertyName = "copyrightlink")] public string CopyRightLink; [JsonProperty(PropertyName = "wp")] public bool WP; [JsonProperty(PropertyName = "hsh")] public string HSH; [JsonProperty(PropertyName = "drk")] public int DRK; [JsonProperty(PropertyName = "top")] public int Top; [JsonProperty(PropertyName = "bot")] public int Bot; [JsonProperty(PropertyName = "hs")] public List<HS> HSs; [JsonProperty(PropertyName = "msg")] public List<Msg> Message; } public class HS { [JsonProperty(PropertyName = "desc")] public string DESC; [JsonProperty(PropertyName = "link")] public string Link; [JsonProperty(PropertyName = "query")] public string Query; [JsonProperty(PropertyName = "locx")] public int Locx; [JsonProperty(PropertyName = "locy")] public string Locy; } public class Msg { [JsonProperty(PropertyName = "title")] public string Title; [JsonProperty(PropertyName = "link")] public string Link; [JsonProperty(PropertyName = "text")] public string Text; } public class ToolTip { [JsonProperty(PropertyName = "loading")] public string Loading; [JsonProperty(PropertyName = "previous")] public string Previous; [JsonProperty(PropertyName = "next")] public string Next; [JsonProperty(PropertyName = "walle")] public string Walle; [JsonProperty(PropertyName = "walls")] public string Walls; } }
第四步:将Json网页内容反序列化到类。
BingPicture today = JsonConvert.DeserializeObject<BingPicture>(jsonContent);