在日常的编码中,经常会遇到JSON类型的数据,有简单的,也有复杂的。对于简单的,我们可以用正则等匹配,但是一旦遇到复杂的,就比较难办了。

数据分析

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

{"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":"28","weatherID":"02转01","windDir":"0","windPower":"0","Tmin":"18"},
{"date":"2013-9-13 20:00:00","Tmax":"33","weatherID":"00","windDir":"0","windPower":"0","Tmin":"18"},
{"date":"2013-9-14 20:00:00","Tmax":"35","weatherID":"00","windDir":"0","windPower":"0","Tmin":"19"},
{"date":"2013-9-15 20:00:00","Tmax":"27","weatherID":"01","windDir":"0","windPower":"0","Tmin":"16"},
{"date":"2013-9-16 20:00:00","Tmax":"25","weatherID":"01","windDir":"0","windPower":"0","Tmin":"17"},
{"date":"2013-9-17 20:00:00","Tmax":"26","weatherID":"02","windDir":"0","windPower":"0","Tmin":"18"},
{"date":"2013-9-18 20:00:00","Tmax":"27","weatherID":"02转07","windDir":"0","windPower":"0","Tmin":"16"}],
"zhishu":[{"value":"2","name":"CY"},
{"value":"0","name":"ZS"},
{"value":"8","name":"FH"},
{"value":"2","name":"ZWX"},
{"value":"4","name":"KQWR"},
{"value":"2","name":"LY"},
{"value":"1","name":"JT"},
{"value":"1","name":"GM"},
{"value":"1","name":"SSD"}],
"currentMessage":{"reportTime":"2013-9-12 13:00:00",
"weatherID":"02",
"temperature":"27",
"windDir":"4",
"windPower":"0",
"humidity":"69.0",
"visibility":"8",
"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数据解析的一种思路的更多相关文章

  1. Android JSON原生解析的几种思路,以号码归属地,笑话大全,天气预报为例演示

    Android JSON原生解析的几种思路,以号码归属地,笑话大全,天气预报为例演示 今天项目中要实现一个天气的预览,加载的信息很多,字段也很多,所以理清了一下思路,准备独立出来写一个总结,这样对大家 ...

  2. IOS - JSON数据解析 小3种方法

    [manager GET:serverURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject ...

  3. [转]JSon数据解析的四种方式

    转至http://blog.csdn.net/enuola/article/details/7903632 作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的js ...

  4. JSON数据解析 基础知识及链接收集

    JSON数据解析学习 JSON介绍 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式. JSON 是存储和交换文本信息的语法.类似 XML.但是JSON 比 ...

  5. 浅谈JSON数据解析方法

    JSON数据解析 JSON是什么?? 如何把JSON数据解析出来 如何把一个字典转换为JSON JSON详细介绍 JSON(JavaScript Object Notation) 是一种轻量级的数据交 ...

  6. JSON数据解析(转)

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种理想的数据交换格式. 本文将主要介绍在Android ...

  7. JSON数据解析(GSON方式) (转)

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种理想的数据交换格式. 在上一篇博文<Andro ...

  8. iOS - JSON 数据解析

     iOS - JSON 数据解析 前言 NS_CLASS_AVAILABLE(10_7, 5_0) @interface NSJSONSerialization : NSObject @availab ...

  9. Silverlight项目笔记7:xml/json数据解析、TreeView、引用类型与数据绑定错误、图片加载、虚拟目录设置、silverlight安全机制引发的问题、WebClient缓存问题

    1.xml/json数据解析 (1)xml数据解析 使用WebClient获取数据,获取到的数据实例化为一个XDocument,使用XDocument的Descendants(XName)方法获得对应 ...

随机推荐

  1. iOS UIButton添加圆角,添加边框

    //准备工作 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(,, ...

  2. C语言-04-函数

    函数 函数是一组一起执行任务的语句,函数是一个可执行C程序必不可少的条件(至少一个main()函数),函数的定义形式 returnType functionName() { bodyOf of the ...

  3. Asp.net mvc中的Ajax处理

    在Asp.net MVC中的使用Ajax, 可以使用通用的Jquery提供的ajax方法,也可以使用MVC中的AjaxHelper. 这篇文章不对具体如何使用做详细说明,只对于在使用Ajax中的一些需 ...

  4. Effective Java 25 Prefer lists to arrays

    Difference Arrays Lists 1 Covariant Invariant 2 Reified at runtime Erased at run time 3 Runtime type ...

  5. C#获取HTML文件指定DIV内容

    最近自己找了一个开源的博客网站,放到阿里云上,方便自己发布博客. 我一般把文章发布到博客园和QQ空间,家了这个网站后又要多发布一次,为了省事就做了一个从博客园读取文章的功能: 输入链接URL地址点击提 ...

  6. SQL Server 2008 R2——VC++ ADO 操作 存储过程

    ==================================声明================================== 本文原创,转载在正文中显要的注明作者和出处,并保证文章的完 ...

  7. Github学习进阶-初露锋芒,通过命令行将本地git仓库推送到Github上面的仓库

    前提: 1. 需要安装git 客户端.  能打开 git  bash 命令行窗口. 2. 生成了ssh 秘钥,并添加到了Github上面. 一.在Github上面建立一个git仓库. 点击 + 号,在 ...

  8. Less里css表达式的写法

    项目中用的grunt-contrib-less, 写了以下less代码 .mapfix{ position: fixed; top:10px; width: 430px; z-index: 100; ...

  9. cnblogs美化及插件

    1.vp计数 http://www.amazingcounters.com 2.来源地图 http://clustrmaps.com 2.1来源地图 http://www.flagcounter.co ...

  10. cri-o 创建非infra容器

    1.// cri-o/server/container.go // CreateContainer creates a new container in specified PodSandbox fu ...