数据分析

目前手头上需要制作一个天气预报功能,现成的接口已经有了。我随便输入一个城市,然后出现了如下的信息:

{"wdata":{"cityName":"鹤壁",
"location":{"lat":"35.62",
"lng":"114.18"},
"today":"2013-9-12 10:30:00",
"sevDays":[{"date":"2013-9-12 20:00:00","Tmax":"","weatherID":"02转01","windDir":"","windPower":"","Tmin":""},
{"date":"2013-9-13 20:00:00","Tmax":"","weatherID":"","windDir":"","windPower":"","Tmin":""},
{"date":"2013-9-14 20:00:00","Tmax":"","weatherID":"","windDir":"","windPower":"","Tmin":""},
{"date":"2013-9-15 20:00:00","Tmax":"","weatherID":"","windDir":"","windPower":"","Tmin":""},
{"date":"2013-9-16 20:00:00","Tmax":"","weatherID":"","windDir":"","windPower":"","Tmin":""},
{"date":"2013-9-17 20:00:00","Tmax":"","weatherID":"","windDir":"","windPower":"","Tmin":""},
{"date":"2013-9-18 20:00:00","Tmax":"","weatherID":"02转07","windDir":"","windPower":"","Tmin":""}],
"zhishu":[{"value":"","name":"CY"},
{"value":"","name":"ZS"},
{"value":"","name":"FH"},
{"value":"","name":"ZWX"},
{"value":"","name":"KQWR"},
{"value":"","name":"LY"},
{"value":"","name":"JT"},
{"value":"","name":"GM"},
{"value":"","name":"SSD"}],
"currentMessage":{"reportTime":"2013-9-12 13:00:00",
"weatherID":"",
"temperature":"",
"windDir":"",
"windPower":"",
"humidity":"69.0",
"visibility":"",
"pressure":"1012.2",
"sunrise":"6:01",
"sunset":"18:38"}
}
}

这段JSON数据结构比一般的要复杂那么一点,不过从其结构来看:

第一层应该是wdata。

第二层是cityName(城市名称),location(经纬度),today(当前时间),sevDays(后续天气),zhishu(指数),currentMessage(当前预报信息)。

第三层是:location下面的lat,lng;sevDays下面的date,Tmax,weatherID,windDir,windPower,Tmin; 然后是zhishu下面的value 和 name;最后是currentMessage下面的reportTime,weatherID,temperature,windDir,windPower,humidity,visibility,pressure,sunrise,sunset信息:

所以,总共说来,这个JSON数据总共就三层。

解析方式

那么,如何来解析呢?

其实,我们完全可以从最底层的结构分析起来,然后简历相关的类,最后把这些建立的类组合成类似json数据的结构就可以了。

这里,最底层就是第三层,我们开始建立起相关的类对象:

对于sevDays下的项目, 建立如下类:

using System;

namespace Nxt.Common.Weather
{
public class DateReleation
{
//sevDays
public DateTime date { get; set; }
public int Tmax { get; set; }
public string weatherID { get; set; }
public int windDir { get; set; }
public int windPower { get; set; }
public int Tmin { get; set; }
}
}

对于zhishu下的项目,建立的类如下:

namespace Nxt.Common.Weather
{
public class IndexPoint
{
//zhishu
public int value { get; set; }
public string name { get; set; }
}
}

对于currentMessage下的项目,建立的类如下:

using System;

namespace Nxt.Common.Weather
{
public class CurrentMessage
{
//currentMessage
public DateTime reportTime { get; set; }
public string weatherID {get;set;}
public double temperature { get; set; }
public string windDir { get; set; }
public string windPower { get; set; }
public double humidity { get; set; }
public string visibility { get; set; }
public double pressure { get; set; }
public string sunrise { get; set; }
public string sunset { get; set; }
}
}

对于location下面的项目,建立的类如下:

namespace Nxt.Common.Weather
{
public class Location
{
//location
public string lat { get; set; }
public string lng { get; set; }
}

当第三层的都建立完毕后,现在来建立第二层,第二层的对象如上面所述,但是需要注意的是,sevDays,zhishu都是可以有多条记录的 ,所以我们得用List对象来保存。

using System;
using System.Collections.Generic; namespace Nxt.Common.Weather
{
public class WeatherMain
{
//wdata
public string cityName { get; set; }
public Location location { get; set; }
public DateTime today { get; set; }
public List<DateReleation> sevDays { get; set; }
public List<IndexPoint> zhishu { get; set; }
public CurrentMessage currentMessage { get; set; } public WeatherMain()
{
sevDays = new List<DateReleation>();
zhishu = new List<IndexPoint>();
}
}
}

上面的代码是依据JSON数据的结构而建立的,这样能够最大程度避免数据的不准确性。
最后,建立顶层的类:

namespace Nxt.Common.Weather
{
public class Daemon
{
public WeatherMain wdata { get; set; }
}
}

这样,我们的类结构就建立完毕了。

最后审查一下我们建立的类结构,是不是和JSON数据的组织结构是一样的呢?

如果是一样的,让我们进入下一步:

using System;
using System.IO;
using System.Net;
using System.Web.Script.Serialization;
using Nxt.Common.Weather;
using System.Text; namespace Nxt.Web.Code
{
public class WeatherDaemon
{
public Daemon GetWeather(string areaName)
{
string url = "http://weather.****.net/Weather/getWeather.php?area=" + areaName;
WebRequest request = WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream(); string weatherData = string.Empty;
if (dataStream != null)
{
try
{
using (StreamReader reader = new StreamReader(dataStream, Encoding.UTF8))
{
weatherData = reader.ReadToEnd();
}
}
catch (OutOfMemoryException oe)
{
throw new Exception(oe.Data.ToString());
}
catch (IOException ie)
{
throw new Exception(ie.Data.ToString());
}
} if (!String.IsNullOrEmpty(weatherData))
{
JavaScriptSerializer ser = new JavaScriptSerializer();
Daemon main = ser.Deserialize<Daemon>(weatherData);
return main;
}
return null;
}
}
}

注:使用JavaScriptSerializer,我们需要引用System.web.extensions.

Asp.net Json 解析 与 直接用ip访问返回josn的更多相关文章

  1. PLSQL Developer 直接用ip访问指定数据库

  2. 在nginx中配置如何防止直接用ip访问服务器web server及server_name特性讲解

    看了很多nginx的配置,好像都忽略了ip直接访问web的问题,不利于SEO优化,所以我们希望可以避免直接用IP访问网站,而是域名访问,具体怎么做呢,看下面. 官方文档中提供的方法: If you d ...

  3. 配置Nginx防止直接用IP訪问Webserver

    看了非常多Nginx的配置,好像都忽略了ip直接訪问Web的问题.这样理论上不利于SEO优化,所以我们希望能够避免直接用IP訪问站点.而是域名訪问.详细怎么做呢.看以下. 官方文档中提供的方法: If ...

  4. 【ask】vmware(NAT)中的linux突然无法访问互联网网址,但是直接用ip可以访问。

    前两天虚拟机里的linuxmint不知何故,突然无法访问互联网了.依稀记得是升级了win7下面的360安全卫士之后发生的事情.所以, 第1步就开始去找防火墙的各种设置,结果没有查到结果. 第2步猛然看 ...

  5. Delphi使用JSON解析调用淘宝IP地址库REST API 示例

    淘宝IP地址库:http://ip.taobao.com,里面有REST API 说明. Delphi XE 调试通过,关键代码如下: var IdHTTP: TIdHTTP; RequestURL: ...

  6. Linux tomcat 去除项目名端口号直接用ip或者域名访问网站

    网站开发过程中,一般的工程访问路径是  http://10.10.10.10:8080/projectName如何设置成http://10.10.10.10/ 解决方法: 首先,进入tomcat的安装 ...

  7. 防恶意解析,禁止用IP访问网站的Apache设置

    一般来说,网站可以用域名和IP来访问.你的网站可以通过IP直接访问,本来这没什么问题,但是会有些隐患: 由于搜索引擎也会收录你的IP地址的页面,所以同一个页面搜索引擎会重复收录,造成页面的权重不如单个 ...

  8. 防恶意解析,禁止用IP访问网站的Apache设置 修改 httpd.conf 实现

    一般来说,网站可以用域名和IP来访问.你的网站可以通过IP直接访问,本来这没什么问题,但是会有些隐患: 由于搜索引擎也会收录你的IP地址的页面,所以同一个页面搜索引擎会重复收录,造成页面的权重不如单个 ...

  9. iOS 自己封装的网络请求,json解析的类

    基本上所有的APP都会涉及网络这块,不管是用AFNetWorking还是自己写的http请求,整个网络框架的搭建很重要. 楼主封装的网络请求类,包括自己写的http请求和AFNetWorking的请求 ...

随机推荐

  1. SQL Server性能优化(5)表设计时的注意事项

    一. 是否需要冗余列 现在一些项目的数据库设计中,为了提高查询速度,把基本表的一些列也放到了数据表里,导致数据冗余.例如在热表的数据库里,原始数据表Measure_Heat里加了如房间号,单元号,楼号 ...

  2. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

  3. Linux命令 + Shell

    1. 之前利用Ubuntu14.10的镜像安装了个虚拟机,本以为自己在windows上的就是管理员的权限,就理所当然的认为虚拟的Linux系统也是root权限.而且虽然@符号前的的标识不是root,但 ...

  4. API文档管理工具-数据库表结构思考.

    API文档管理工具-数据库表结构思考. PS: 管理工具只是为了方便自己记录API的一些基本信息,方便不同的开发人员 (App Developer, Restful API Developer)之间的 ...

  5. 获取iframe中的元素

    父窗口中获取iframe中的元素 var ifr = document.getElementById('suggustion').contentWindow.document.body; 在ifram ...

  6. 手写PE文件(一)

    DOS Header(IMAGE_DOS_HEADER)->64 Byte DOS头部 DOS Stub 112字节 "PE"00(Signature) 4个字节 IMAGE ...

  7. 请教DotNetBar控件中的CalendarView控件如何拖动当前的时间轴

    本人想拖动那个当前的时间轴或者让时间轴变动,因为那个时间轴默认的是当前时间.(就是那个黄色的线)

  8. Using an Interface as a Type

    When you define a new interface, you are defining a new reference data type. You can use interface n ...

  9. iOS获取手机相关信息

    iOS具体的设备型号: #include <sys/types.h> #include <sys/sysctl.h> - (void)test { //手机型号. size_t ...

  10. ExtJs之addManagedListener

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...