数据分析

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

{"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.

最新文章

  1. 将String转化成Stream,将Stream转换成String
  2. 用Qt写软件系列三:一个简单的系统工具(上)
  3. 常用 Git 命令清单(摘录)
  4. Java for LeetCode 168 Excel Sheet Column Title
  5. mysql开机脚本
  6. SSL简介
  7. 通过 DynamicLinq 简单实现 N-Tier 部署下的服务端数据库通用分页
  8. 【python】列表
  9. 五、Pyqt5事件、信号和槽
  10. JVM内存分配和垃圾收集策略
  11. Python_每日习题_0003_完全平方数
  12. ansible jenkins war
  13. error C1128: 节数超过对象文件格式限制: 请使用 /bigobj 进行编译
  14. Java 避免创建不必要的对象
  15. spring整合kafka(配置文件方式 生产者)
  16. Maven_2 本地资源库 中央存储库
  17. CodeForces 727C
  18. lamda匿名函数(与sorted(),filter(),map() 一起用), 递归函数, 二分查找
  19. java中break和continue跳出指定循环(转载)
  20. 【Android UI设计与开发】第01期:引导界面(一)ViewPager介绍和使用详解

热门文章

  1. 【CentOs】配置nginx
  2. 【Dancing Link专题】解题报告
  3. poi实现Excel比较
  4. sigaction 函数
  5. Topcoder 多校T-shirt场
  6. zero to one:创业秘籍并不存在,因为任何创新都是新颖独特的,任何权威都不可能具体规定如何创新
  7. Solr笔记--转载
  8. iOS模型以及使用
  9. Content Providers详解
  10. Hibernate3.6中文手册