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. vmware目录2

    http://www.globalknowledge.com/training/course.asp?pageid=9&courseid=17880&country=United+St ...

  2. VMware用于Site Recovery Manager 5的vSphere Replication功能一览

    http://www.searchstorage.com.cn/showcontent_54838.htm 参考:深度解析SRM 5.0和vSphere Replication http://wenk ...

  3. JMX入门开发

    什么是JMX?或者是JMX是做什么的?我的理解是:可以远程管理/编辑JAVA对象.如图: 上面的SchemaName属性就是可以动态修改的,那么是如何做到的哪?下面咱们逐步分析. 一.首先假设咱们有个 ...

  4. Cocos开发中可能会遇到的问题

      开发中碰到的问题及解决方案: 1:场景工程中没有被依赖关联的图片声音或者其它资源,导出到微信后找不到 有些在场景工程中没有指定而通过代码中动态加载的资源,cocos creator不会导出到发布目 ...

  5. VB控件 与 引用或部件

    序号 控件名 部件或引用 用途 2 ActiveMovie Microsoft ActiveMovie Control    3 ADODB Windows ADO Ext. 2.8 for DLL ...

  6. secureCRT,永久设置,保护眼睛,配色方案

    配色后效果如下: 下面开始配色 1.选项(Options)==>会话选项(Sessions options)==>终端(Terminal)==>仿真(Emulation) 按图中标注 ...

  7. Echarts柱状图的点击事件

    最近在做一些图表统计的功能,用到了百度的开源图表软件Echatrs,不得不提的是:不但上手简单而且扩展功能也是十分强大.在使用的过程中也遇到了不少问题,可能由于有关Echatrs的资料并不是很齐全,所 ...

  8. Linux下grub.cnf详解

    grub.conf跟系统启动项有关,对于重置密码.来说小case... 1.介绍    在Red Hat Linux7.2之后,默认的引导加载程序从LTLO变为GRUB.这个引导加载程序使用户能够选择 ...

  9. 在Java中使用Kafka

    Producer部分 Producer在实例化后, 对外提供send方法, 用于将数据送到指定的topic和partition; 以及在退出时需要的destroy方法. 接口 KafkaProduce ...

  10. 转:折腾一晚上Bullet及Ogre相关工具的成果 -- 3Ds Max,Maya, blender, GameKit

    起始目的很简单,整合Bullet及Ogre,找个能够生成.scene和.bullet文件的建模工具. 折腾一晚上Bullet及Ogre相关的东西,基本上就像爱迪生发明灯泡一样,得出了N个失败的教训,总 ...