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. asp.net正则表达式

    导入引用命名空间:using System.Text.RegularExpressions //Regex类,常用方法: //摘要:1.IsMatch(String);2.IsMatch(String ...

  2. APP测试体系

    网上找的图片,总结的很好:

  3. Excel常用快捷键大全

    一.关于处理工作表的快捷键总结 1.插入新工作表 Shift+F11或Alt+Shift+F1 2.移动到工作簿中的下一张工作表 Ctrl+PageDown 3.移动到工作簿中的上一张工作表 Ctrl ...

  4. 2016 博客导读总结 &amp; 个人感悟

    此文着笔之时.2017已经在眼前了.预计等我写完,2017已经到了. 二次编辑于2017年1月1日早11点. 关于2016的感悟.十二月初就想写,当时认为是有点太早了,只是却思绪如泉涌. 且那时候才刚 ...

  5. Linux下利用signal函数处理ctrl+c等信号

    前言 linux下能够通过信号机制来实现程序的软中断,是一个很实用的编程方法. 我们平时在程序执行的时候按下ctrl-c.ctrl-z或者kill一个进程的时候事实上都等效于向这个进程发送了一个特定信 ...

  6. Android学习笔记一:项目目录结构

    一:Android目录 主要内容有: app目录下: manifests目录: AndroidManifest.xml:APP的配置 java目录:主要为源代码和测试代码 res目录:主要是资源文件, ...

  7. Lotus Domino开发心得(一)

    —- Lotus Domino 是当今办公自动化系统的主流开发平台之一,目前大部分企业和机构都在使用Lotus Domino 开发自己的无纸办公系统.在开发过程中,我积累了一些小技巧,现在公布出来,希 ...

  8. NSDictionary的分类

    @implementation NSDictionary (extra) //根据key值的到字典中的object - (id)getObjectByKey:(NSString*)key { NSAr ...

  9. Deep Introduction to Go Interfaces.

    Standard Interface Intro Go’s interfaces are one of it’s best features, but they’re also one of the ...

  10. MATLAB 向量

    MATLAB 向量: 1.MATLAB 行向量: 创建行向量括在方括号中的元素的集合,用空格或逗号分隔的元素. 2.MATLAB 列向量: 创建列向量括在方括号中的元素的集合,使用分号来分隔的元素. ...