如何使用JSON Web服务.. [英] How to consume a JSON web service..

查看:105
本文介绍了如何使用JSON Web服务..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近学会了如何使用动手实验来使用XML格式的Web服务。不幸的是,我正在查询的服务器以JSON格式返回查询。

I recently learned how to consume a web services in XML format with the hands on lab. Unfortunately the server I'm querying returns queries in JSON format.

 

如果有人可以向我展示以下XML Web的JSON等价物服务消费代码,这将是一个巨大的开始。

If anyone can show me the JSON equivalent of the following XML web service consumption code, that'd be a huge start.

 

 


public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

	    void MainPage_Loaded(object sender, RoutedEventArgs e)
	    {
	        FetchBingPics("Seattle", PhotoList);
	    }

        public async void FetchBingPics(string strKeyWord, object control)
	    {
            String strBingAppID = "KEY HERE";
            XNamespace xmlns =
                "http://schemas.microsoft.com/LiveSearch/2008/04/XML/multimedia";
            HttpClient client = new HttpClient();

            HttpResponseMessage response = await
                client.GetAsync("http://api.bing.net/xml.aspx?AppId=" + strBingAppID +
                "&Version=2.0&Market=en-us&Image.Count=50&Sources=Image&Query=" +
                strKeyWord);

            string xml = response.Content.ReadAsString();

            XDocument doc = XDocument.Parse(xml);

            IEnumerable<BingImage> images = from img in doc.Descendants(
	        xmlns + "ImageResult")
	        where !img.Element(xmlns + "MediaUrl").Value.EndsWith(".bmp")
	        select new BingImage
	            {
	                Title = img.Element(xmlns + "Title").Value,
	                MediaUrl = img.Element(xmlns + "MediaUrl").Value
	            };
            if (control is ListBox)
	            (control as ListBox).ItemsSource = images;
	        else
	            (control as GridView).ItemsSource = images;

推荐答案

Hi Matt,

Hi Matt,

您可以使用Windows .Data.Json对象来解析从站点返回的Json数据。 另外,请查看DataContractJsonSerializer类,以简化将json数据转换为.Net类。

You can use the Windows.Data.Json objects to parse the Json data you get back from the site.  Also take a look at the DataContractJsonSerializer class to simplify converting the json data into a .Net class.

- Rob


这篇关于如何使用JSON Web服务..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆