团购API解析 c#实现

晨曦之光 发布于 2012/05/23 11:03
阅读 109
收藏 0

<response code="0" date="2011-07-10T18:36:03+08:00" message="Ok"><deals><deal><id>366723</id><deal_url>http://nj.meituan.com/deal/366723.html</deal_url><title>仅售99元!原价280元的首尔家韩国料理异域风情韩国料理4人套餐(石板炒鸡排+石板泡菜五花肉+石板烤鱿鱼+嫩豆腐汤+海带汤+土豆饼+海鲜葱饼+石锅拌饭+炒年糕+炒饭2份),另送小菜4份+生菜+烤肉酱4份,原“一只鸡”韩国料理上海路店。锅内的肉片“吱吱”冒烟,烤熟的浮香袅袅升烟,味蕾旅程瞬间开启,美食路上邂逅异域风情,约定首尔家,悸动相伴一路驰骋!</title><small_image_url>http://p1.meituan.net/120.76/deal/201107/08/_0708113345.jpg</small_image_url><medium_image_url>http://p0.meituan.net/200.121/deal/201107/08/_0708113345.jpg</medium_image_url><large_image_url>http://p1.meituan.net/deal/201107/08/_0708113345.jpg</large_image_url><division_id>nanjing</division_id><division_name>南京</division_name><division_lat>32.058365</division_lat><division_lng>118.796468</division_lng><division_timezone>Beijing Time</division_timezone><division_offset_gmt>288000</division_offset_gmt><vendor_id>首尔家韩国料理</vendor_id><vendor_name>首尔家韩国料理</vendor_name><vendor_website_url/><status>open</status><start_date>2011-07-10T00:00:00+08:00</start_date><end_date>2011-07-12T23:59:59+08:00</end_date><tipped>true</tipped><tipping_point>10</tipping_point><tipped_date>2011-07-10T07:30:32+08:00</tipped_date><sold_out>false</sold_out><quantity_sold>100</quantity_sold><price>99</price><value>280</value><discount_amount>181</discount_amount><discount_percent>3.5折</discount_percent><areas>
      </areas><conditions><limited_quantity>true</limited_quantity><minimum_purchase>1</minimum_purchase><maximum_purchase>100</maximum_purchase><expiration_date>2011-09-10T23:59:59+08:00</expiration_date><details><detail>有效期截止至2011年9月10日; 自2011年7月13日起接待; 
营业时间:11:00-凌晨4:00; 
请提前1-2天致电025-83205515预约; 
不可同时享受店内其他优惠; </detail></details></conditions></deal>

上面是美团网提供的API借口,可以看出是一XML文档形式存在的。主要标签结构如下

<response>

         <deals>

                    <deal>

                                  <title></title>

                                  <deal_url></deal_url>

                                  ......................

                                 每则团购信息存放在这里,里面有title,deal_url等相关信息标签

                   </deal>

                   <deal>

 

                   </deal>

                。。。。。。


        </deals>

</response>

我们主要做的就是解析他提供的XML文档,读取出我们所需要的团购信息存入相应变量中即可

1.设计存放我们需要的团购信息的数据结构

        public class Deal
        {
            public string Title;
            public string ImageUrl;
            public string price;
            public string value;
            public string DealUrl;
        }

         这个具体可以根据实际需要的内容自行设计~~

2.读取美团网提供的XML文档

       XmlTextReader Reader = new XmlTextReader("http://www.meituan.com/api/v1/nanjing/deals");
            // XmlValidatingReader Valid = new XmlValidatingReader(Reader);
            // Valid.ValidationType = ValidationType.None;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Reader);
            XmlNode ResponseNode = FoundChildNode(xmlDoc, "response");
            XmlNode DealsNode = FoundChildNode(ResponseNode, "deals");
            List<Deal> Deal = new List<Deal>();
            for (int i = 0; i < DealsNode.ChildNodes.Count; i++)
            {
                switch (DealsNode.ChildNodes[i].Name)
                {
                    case "deal":
                        {
                            Deal temp=getRssItem(DealsNode.ChildNodes[i]);
                            Deal.Add(temp);
                            break;
                        }
                }
            }

      其中FoundChildNode:找到提供结点的相应子节点

            getRssItem:找到提供结点所有的子节点,大家可以自己去试着去实现,也可以参考我的,我会在文章最后提供我的整个源码

下面的是我写的源码,我是做成Web Server的,供大家参考下吧:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml;

namespace WebApplication1
{
    /// <summary>
    /// Rss 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class Rss : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

       
        [WebMethod]
        public List<Deal> ReadXml()
        {
            XmlTextReader Reader = new XmlTextReader("http://www.meituan.com/api/v1/nanjing/deals");
            // XmlValidatingReader Valid = new XmlValidatingReader(Reader);
            // Valid.ValidationType = ValidationType.None;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Reader);
            XmlNode ResponseNode = FoundChildNode(xmlDoc, "response");
            XmlNode DealsNode = FoundChildNode(ResponseNode, "deals");
            List<Deal> Deal = new List<Deal>();
            for (int i = 0; i < DealsNode.ChildNodes.Count; i++)
            {
                switch (DealsNode.ChildNodes[i].Name)
                {
                    case "deal":
                        {
                            Deal temp=getRssItem(DealsNode.ChildNodes[i]);
                            Deal.Add(temp);
                            break;
                        }
                }
            }
            return Deal;

        }
        private XmlNode FoundChildNode(XmlNode Node, string Name)
        {
            XmlNode childlNode = null;
            for (int i = 0; i < Node.ChildNodes.Count; i++)
            {
                if (Node.ChildNodes[i].Name == Name && Node.ChildNodes[i].ChildNodes.Count > 0)
                {
                    childlNode = Node.ChildNodes[i];
                    return childlNode;
                }
            }
            return childlNode;
        }
        private Deal getRssItem(XmlNode Node)
        {
            Deal item = new Deal();
            for (int i = 0; i < Node.ChildNodes.Count; i++)
            {
                if (Node.ChildNodes[i].Name == "title")
                {
                    item.Title = Node.ChildNodes[i].InnerText;
                }
                else if (Node.ChildNodes[i].Name == "deal_url")
                {
                    item.DealUrl = Node.ChildNodes[i].InnerText;
                }
                else if (Node.ChildNodes[i].Name == "medium_image_url")
                {
                    item.ImageUrl = Node.ChildNodes[i].InnerText;
                }
                else if (Node.ChildNodes[i].Name == "price")
                {
                    item.price = Node.ChildNodes[i].InnerText;
                }
                else if (Node.ChildNodes[i].Name == "value")
                {
                    item.value = Node.ChildNodes[i].InnerText;
                }
            }
            return item;
        }   


        /// <summary>
        /// 数据成员
        /// </summary>
       
        public class Deal
        {
            public string Title;
            public string ImageUrl;
            public string price;
            public string value;
            public string DealUrl;
        }
    }
}



原文链接:http://blog.csdn.net/txg703003659/article/details/6596337
加载中
返回顶部
顶部