C# 获取接口数据(xml格式)转为json格式

流程:XmlDocument-->XmlNode -->XmlNodeList
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Net;
using System.IO;

namespace juheWeather
{
    public partial class BJweather : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //string callback = Request.QueryString["callback"];
            Response.Write(getWeather());
            Response.End();
        }

        public string getWeather()
        {
            string weatherXML = GetRequestData("http://flash.weather.com.cn/wmaps/xml/beijing.xml");
            XmlDocument xml = new XmlDocument();(深入研究获取xml各种位置的信息,请百度:XmlDocument,还可以参见 readXML实现程序
            xml.LoadXml(weatherXML);
            XmlNode root = xml.SelectSingleNode("beijing");
            XmlNodeList childlist = root.ChildNodes;
            string strResult = "[";
            for (int i = 0; i < childlist.Count; i++)
            {
                strResult += "{'cityname':'" + childlist[i].Attributes["cityname"].Value + "',";
                strResult += "'state1':'" + childlist[i].Attributes["state1"].Value + "',";
                strResult += "'state2':'" + childlist[i].Attributes["state2"].Value + "',";
                strResult += "'stateDetailed':'" + childlist[i].Attributes["stateDetailed"].Value + "',";
                strResult += "'tem1':'" + childlist[i].Attributes["tem1"].Value + "',";
                strResult += "'tem2':'" + childlist[i].Attributes["tem2"].Value + "',";
                strResult += "'temNow':'" + childlist[i].Attributes["temNow"].Value + "',";
                strResult += "'windState':'" + childlist[i].Attributes["windState"].Value + "',";
                strResult += "'windDir':'" + childlist[i].Attributes["windDir"].Value + "',";
                strResult += "'windPower':'" + childlist[i].Attributes["windPower"].Value + "',"; 
                strResult += "'humidity':'" + childlist[i].Attributes["humidity"].Value + "',";
                strResult += "'time':'" + childlist[i].Attributes["time"].Value + "',";
                strResult += "'url':'" + childlist[i].Attributes["url"].Value + "'},";
            }
            strResult = strResult.Substring(0, strResult.Length - 1);
            return strResult + "]";
        }

        public static string GetRequestData(string sUrl)
        {
            //使用HttpWebRequest类的Create方法创建一个请求到uri的对象。
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(sUrl);
            //指定请求的方式为Get方式
            request.Method = WebRequestMethods.Http.Get;
            //获取该请求所响应回来的资源,并强转为HttpWebResponse响应对象
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            //获取该响应对象的可读流
            StreamReader reader = new StreamReader(response.GetResponseStream());
            //将流文本读取完成并赋值给str
            string str = reader.ReadToEnd();
            //关闭响应
            response.Close();
            return str;
        }
    }
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值