百度天气

  接口地址:http://api.map.baidu.com/telematics/v3/weather?location=上海&output=json&ak=hXWAgbsCC9UTkBO5V5Qg1WZ9,其中ak是密钥,自行去申请即可,便于大家测试,楼主就公布并了自己的Key,这样可以直接获取到数据。

  获取到的数据是这样的:

{"error":0,"status":"success","date":"2014-10-27","results":[{"currentCity":"上海","pm25":"95","index":[{"title":"穿衣","zs":"较舒适","tipt":"穿衣指数","des":"建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。"},{"title":"洗车","zs":"较适宜","tipt":"洗车指数","des":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"},{"title":"旅游","zs":"适宜","tipt":"旅游指数","des":"天气较好,温度适宜,但风稍微有点大。这样的天气适宜旅游,您可以尽情地享受大自然的无限风光。"},{"title":"感冒","zs":"较易发","tipt":"感冒指数","des":"天气较凉,较易发生感冒,请适当增加衣服。体质较弱的朋友尤其应该注意防护。"},{"title":"运动","zs":"较适宜","tipt":"运动指数","des":"天气较好,但风力较大,推荐您进行室内运动,若在户外运动请注意防风。"},{"title":"紫外线强度","zs":"弱","tipt":"紫外线强度指数","des":"紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。"}],"weather_data":[{"date":"周一 10月27日 (实时:19℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多云","wind":"东北风3-4级","temperature":"21 ~ 16℃"},{"date":"周二","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/yin.png","weather":"多云转阴","wind":"东风微风","temperature":"21 ~ 17℃"},{"date":"周三","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/xiaoyu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png","weather":"小雨","wind":"东风微风","temperature":"21 ~ 19℃"},{"date":"周四","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/xiaoyu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png","weather":"小雨","wind":"东南风微风","temperature":"23 ~ 20℃"}]}]}

  根据返回的Json定义出相应的数据结构:

public class BaiduTQ
{
public int error { get; set; }
public string status { get; set; }
public string date { get; set; }
public List<BaiduResult> results { get; set; }
} public class BaiduResult
{
public string currentCity { get; set; }
public string pm25 { get; set; }
public List<BaiduIndex> index { get; set; }
public List<BaiDuWeaterData> weather_data { get; set; }
} public class BaiduIndex
{
public string title { get; set; }
public string zs { get; set; }
public string tipt { get; set; }
public string des { get; set; }
} public class BaiDuWeaterData
{
public string date { get; set; }
public string dayPictureUrl { get; set; }
public string nightPictureUrl { get; set; }
public string weather { get; set; }
public string wind { get; set; }
public string temperature { get; set; }
}

  然后直接通过Newtonsoft.Json 反序列化成即可。

  既然是获取天气,肯定是希望获取客户所在城市的天气,下一步则是需要根据用户机器IP获取所在城市,然后获取该城市的天气信息。

IP获取城市

  通过淘宝的IP库,http://ip.taobao.com/,即可查询指定IP所在的城市、国家、运营商等。

  有了上面的途径,我们下一步的工作就是获取客户的外网IP,而外网IP,是机器连接外网才会有,所以楼主写了一个页面,部署在外网服务器。

  相关代码如下:

         var ip = Request.UserHostAddress;
using (var client = new WebClient())
{
var url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip;
client.Encoding = Encoding.UTF8;
var str = client.DownloadString(url);
Response.Write(str);
}

  这样我们就可以获取到客户所在城市的天气数据了。

获取百度新闻

  最近还有个小需求,获取某某新闻数据,楼主习惯性的查了下百度的相关资料,能通过Rss来获取百度新闻数据。

  接口地址:http://news.baidu.com/n?cmd=7&loc=0&name=%B1%B1%BE%A9&tn=rss

  打开后,查看它的源,无非就是xml文件,我们可以将xml文件,序列化成对象,如果没有接触过这类知识,可以看下《xml与对象的序列化和反序列化》

  根据它的源,就能轻松定义出数据结构。

    [XmlRoot("rss")]
public class Rss
{
public Channel channel { get; set; }
}
[XmlRoot("channel")]
public class Channel
{
public string title { get; set; }
public BaiduImage image { get; set; }
public string link { get; set; }
public string description { get; set; }
public string language { get; set; }
public string lastBuildDate { get; set; }
public string docs { get; set; }
public string generator { get; set; }
[XmlElement]
public List<Channel_Item> item { get; set; }
} public class BaiduImage
{
public string title { get; set; }
public string link { get; set; }
public string url { get; set; }
} public class Channel_Item
{
public string title { get; set; }
public string link { get; set; }
public string pubDate { get; set; }
public string guid { get; set; }
public string source { get; set; }
public string author { get; set; }
public string description { get; set; }
}

  序列化的方法很简单。

        /// <summary>
/// 反序列化
/// </summary>
public static T Deserialize<T>(string xmlContent)
{
XmlSerializer xs = new XmlSerializer(typeof(T));
using (StringReader strReader = new StringReader(xmlContent))
{
XmlReader xmlReader = XmlReader.Create(strReader);
return (T)xs.Deserialize(xmlReader);
}
}

  

相关测试代码如下:

  一键下载

C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市的更多相关文章

  1. 用andtoid studio获取天气数据并解析适配

    1.申请拿到数据 可以用“聚合数据” 2.在android studio中导入需要的jar包 复制—>app—>libs—>粘贴—>右击—>Add As Library… ...

  2. Python解析SWAN气象雷达数据--(解析、生成ASCII、Image、netCDF)

    解析   from datetime import * import time import calendar import json import numpy as np from struct i ...

  3. 【spring源码学习】springMVC之映射,拦截器解析,请求数据注入解析,DispatcherServlet执行过程

    [一]springMVC之url和bean映射原理和源码解析 映射基本过程 (1)springMVC配置映射,需要在xml配置文件中配置<mvc:annotation-driven >  ...

  4. php根据IP获取所在省份-百度api接口

    这里用的file_put_contents,你也可以用别的,直接怼代码: //拼接传递的参数 $getData = array( 'query' => '127.0.0.1', 'resourc ...

  5. python + docker, 实现天气数据 从FTP获取以及持久化(二)-- python操作MySQL数据库

    前言 在这一节中,我们主要介绍如何使用python操作MySQL数据库. 准备 MySQL数据库使用的是上一节中的docker容器 “test-mysql”. Python 操作 MySQL 我们使用 ...

  6. Android访问中央气象台的天气预报API得到天气数据

      最新说明:该接口已失效! 2014-03-04 可申请它公布的API,需申请:http://smart.weather.com.cn/wzfw/smart/weatherapi.shtml 在用A ...

  7. 百度AI人脸检测——解析JSON 数据格式

    1.了解一下 通常情况下,每个需要访问网络的应用程序都会有一个自己的服务器,我们可以向服务器提交数据,也可以从服务器上获取数据.不过这个时候就出现了一个问题,这些数据到底要以什么样的格式在网络上传输呢 ...

  8. Android解析中国天气网的Json数据

    在Android开发中.一般的APP都是通过获取server端的数据来更新UI.从server获取到的数据能够是Json.它的数据量要比XML要小,这里解析中国天气网上获取的数据,尽管已经不再更新了. ...

  9. Android解析中国天气接口JSon数据,应用于天气查询!

    android解析Json数据是比较常见的一种操作.也是客户端和服务器进行数据交互的桥梁.下面就来看一看在android中解析JSon数据的方法吧. 首先要想获得Json数据,就必须访问相关的网络接口 ...

随机推荐

  1. paip.文件读写api php java python总结.txt

    paip.文件读写api php java python总结.txt 一.多种方式读文件内容.    1.按字节读取文件内容   以字节为单位读取文件,常用于读二进制文件,如图片.声音.影像等文件. ...

  2. SAFS Init Files

    There're many deployment files for configuration. We need to learn how SAFS read these depolyment fi ...

  3. Linux系统中CPU使用率查询常用的5个命令

    在程序开发中,我们一般都是在Linux系统上进行开发,因此对Linux系统的维护工作很重要.在Linux系统维护中,我们需要经常查看的就是cpu的使用率,分析系统的整体运行情况.那CPU使用率怎么查询 ...

  4. iOS开发拓展篇-XMPP简单介绍

    iOS开发拓展篇-XMPP简单介绍 一.即时通讯简单介绍 1.简单说明 即时通讯技术(IM)支持用户在线实时交谈.如果要发送一条信息,用户需要打开一个小窗口,以便让用户及其朋友在其中输入信息并让交谈双 ...

  5. madown标签说明

    1.删除线的用法 ~~这是删除线~~

  6. 转载:Cellebrite携两大移动数据服务强势来华

    [IT168专稿]随着移动互联网的发展,智能终端也越来越普及,围绕整个移动互联网的产业链产生了巨大的商机.有这么一家做移动数据传输服务的厂商,他们一直专注在移动领域,为运营商和零售商以及司法部门提供服 ...

  7. 针对不同的Cookie做页面缓存

    有时我们需要为PC浏览器及移动浏览器生成不同的页面,为了提高性能,不能每次请求都去判断User-Agent,通常用一个 Cookie 标记一下客户端是否是移动客户端,这样只需要读取这个 Cookie ...

  8. SVO实时全局光照优化(里程碑MK2):Sparse Voxel Octree based Global Illumination (SVO GI)

    自主实现的实时渲染引擎,对标对象ue4/ce5,超越u3d/klayge.MK2版本侧重于质量与速度的均衡,以下上传示范均为实测截图,均为全分辨率(网页上显示缩小了)1080p/60fps.

  9. 如何增强 Linux 系统的安全性,第一部分: Linux 安全模块(LSM)简介

    http://www.ibm.com/developerworks/cn/linux/l-lsm/part1/ 1.相关背景介绍:为什么和是什么 近年来Linux系统由于其出色的性能和稳定性,开放源代 ...

  10. Scala 深入浅出实战经典 第52讲:Scala中路径依赖代码实战详解

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...