using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml; namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
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.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;
} }
}

  

C# 获取接口数据(xml格式)转为json格式的更多相关文章

  1. 将object格式转为json格式

    在页面内容显示时,有时需要用到json格式.但数据库内容的显示,需要将数据库中获取的格式转为json: using Newtonsoft.Json;public static string ToJso ...

  2. charles抓取线上接口数据替换为本地json格式数据

    最近要做下拉刷新,无奈测试服务器的测试数据太少,没有足够的数据做下拉刷新,所以用charles抓取了测试服务器的接口,然后在伪造了很多数据返回到我的电脑上,下面来说说使用方法: 第一步: 安装FQ软件 ...

  3. 数据库获取map数据后转化成json格式的数据

    一,从数据库查出来的数据 两张表先各自左外连接,然后在相互左外连接查找省市县的数据(业务需求必须这样做,省市去的是第一张表,而市县取的是第二张表,两张表中间通过市的名字连接)见这个博文的最后一张图片 ...

  4. 读取mysql数据库的数据,转为json格式

    # coding=utf-8 ''' Created on 2016-10-26 @author: Jennifer Project:读取mysql数据库的数据,转为json格式 ''' import ...

  5. 关于多条数据转为json格式单次传输的问题 2017.05.27

    数据形式如下: var mycars = [];//定义数组存放多条数据 for(var i=0;i<2;i++){ var jsonData = {};//定义变量存放单条数据 jsonDat ...

  6. ajax处理返回的三种格式(json格式 , xml通用格式 , html文本格式)(数据类型:整数、字符串、数组、对象)(基础最重要!)

    ajax方法的参数 常用的ajax参数比如url,data,type,包括预期返回类型dataType,发送到服务器的数据的编码类型contentType,成功方法,失败方法,完成方法.除了这些以外还 ...

  7. python中将xml格式转json格式

    一.简介 在用python写脚本时,通常需要处理xml格式的文件或字符串.由于json格式处理的方便性, 我们可将其转为json格式进行处理. 二.步骤 1.安装工具包xmltodict 在命令行输入 ...

  8. C#RSA算法实现+如何将公钥为XML格式转为PEM格式,给object-C使用

    .net中,处于安全的考虑,RSACryptoServiceProvider类,解密时只有同时拥有公钥和私钥才可以.原因是公钥是公开的,会被多人持有.这样的数据传输是不安全的.C#RSA私钥加密,公钥 ...

  9. Thinkphp3.2.3框架下封装公共的函数,例如封装CURL函数来获取接口数据

    当我们需要在控制层调用相同的封装函数时,写多次相同的函数,显得代码十分的拉杂,不精简: TP框架有一个很好的机制,可以再Common定义一个function.php函数,当我们在控制层调用的时候直接调 ...

  10. php curl请求和获取接口数据

    curl请求和获取接口数据 class ToolModel{ /** * [http 调用接口函数] * @Author GeorgeHao * @param string $url [接口地址] * ...

随机推荐

  1. File /hbase could only be replicated to 0 nodes instead of minReplication (=1). There are 30 datanode(s) running and no node(s) are excluded in this operation.

    原因: hdfs-site.xml中的配置为: <property> <name>dfs.datanode.du.reserved</name> <value ...

  2. 微信小程序 - 3d轮播图组件(基础)

    <!-- 目前仅支持data数据源来自banner,请看测试案例 ################ 以上三种形式轮播: 1. basic 2. 3d 3. book basic即普通轮播 3d即 ...

  3. 定制保存top输出信息的格式详解

    top命令的重要性和使用方法不多说了,这里终点讨论如何保存top命令的输出信息.     保存top命令的输出到一个文件的方法是:top -n1b > topinfo.txt,这没什么好奇的,但 ...

  4. Windows Python+Eclipse环境配置

    参考这几篇博客: 1:http://www.cnblogs.com/realh/archive/2010/10/04/1841907.html 2:http://www.cnblogs.com/min ...

  5. SpringMVC学习笔记六:使用Formatter解析或格式化数据

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6832903.html      Converter可以将一种类型转换成另一种类型,是任意Object之间的类型 ...

  6. 理解Android编译命令(转)

    一.引言 关于Android Build系统,这个话题很早就打算整理下,迟迟没有下笔,决定跟大家分享下.先看下面几条指令,相信编译过Android源码的人都再熟悉不过的. source setenv. ...

  7. Oracle Data Integrator 12c----包(Package)

    1 创建"包" Designer->项目->ODI_Exercise ->第一个文件夹->包,右键"新建程序包": "定义&q ...

  8. python之模块datetime 常见操作

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块datetime #http://blog.sina.com.cn/s/blog_6c37 ...

  9. jQuery方法区别(四)click() bind() live() delegate()区别

        click(),bind(),live()都是执行事件时使用的方法,他们之前是有一些区别的,我们在使用这些方法时应该根据需要进行选择. 1.click()方法是我们经常使用的单击事件方法: $ ...

  10. jquery .live() .delegate() .bind() .click()区别

    什么是.live()? 除了让你对Dom元素现在和将来绑定事件之外,.live() 方法和.bind()方法很像.你可以用.live()方法对没有存在的Dom节点绑定事件.考虑下面的情况. 当用户要离 ...